.htaccess中加入斜线,并迫使WWW用干净的网址 [英] htaccess add trailing slash and force www with clean urls

查看:150
本文介绍了.htaccess中加入斜线,并迫使WWW用干净的网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用我的htaccess文件mod_rewrite的创造清洁的网址是这样的:

I'm using my htaccess file with mod_rewrite to create clean urls like this:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
</IfModule>

我也想迫使该网站有WWW的子域,最importunately添加斜线如果URL没有之一。

I would also like to force the site to have the 'www' subdomain and most importunately add a trailing slash if the url doesn't have one.

我是一个绝对的小白与mod_rewrite的,我已经尝试过通过合并其他code,我发现在谷歌(悲伤,我知道)我自己完成这一点,但我总是最后一个500错误。

I am an absolute noob with mod_rewrite and I've tried accomplishing this on my own by combining other code I found on google (sad I know), but I always end up with a 500 error.

这里的code我发现力WWW:

Here's the code I found for force www:

<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteCond %{HTTP_HOST} ^www\.domain\.tld$ [NC]
RewriteRule ^(.*)$ http://domain.tld/$1 [R=301,L]
</IfModule>

感谢您的帮助。

Thanks for your help.

推荐答案

尝试分离出来的WWW和结尾的斜线检查。这是测试,希望为你工作。你没有说,如果你正在运行放置在域根目录或子目录 - 通常是良好的信息寻求帮助htaccess的时候

Try separating out the www and the trailing slash check. This is tested and hopefully working for you. You didn't say if you're running placing at domain root or in a subdirectory - usually good info when asking for help with htaccess.

RewriteEngine On

# Assuming you're running at domain root.  Change to working directory if needed.
RewriteBase /

#
# www check
# If you're running in a subdirectory, then you'll need to add that in
# to the redirected url (http://www.mydomain.com/subdirectory/$1

RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [R=301,L]

#
# Trailing slash check

# Don't fix direct file links
RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ $1/ [L,R=301]

#
# Finally, forward everything to your front-controller

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [QSA,L]

要调试,注释掉的各个部分,看看是什么/是行不通的。

To debug, comment out the individual sections and see what is/isn't working.

这篇关于.htaccess中加入斜线,并迫使WWW用干净的网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆