我正在使用我的htaccess文件和mod_rewrite来创建这样的干净网址:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
</IfModule>
我还想强制该网站拥有'www'子域名,并且最重要的是如果网址没有,则添加一个尾随斜杠。
我是mod_rewrite的绝对菜鸟,我已经尝试通过组合我在google上找到的其他代码来实现这一点(我很遗憾),但我总是以500错误结束。
以下是我为force www发现的代码:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteCond %{HTTP_HOST} ^www\.domain\.tld$ [NC]
RewriteRule ^(.*)$ http://domain.tld/$1 [R=301,L]
</IfModule>
感谢您的帮助。
答案 0 :(得分:11)
尝试分离出www和斜杠检查。经过测试,希望能为您服务。你没有说你是在运行放置在域根目录还是在子目录中 - 通常是在向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]
要进行调试,请注释掉各个部分,看看哪些/不起作用。
答案 1 :(得分:1)
使用此并忘记您的问题;)
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*?)/*$ http://%1/$1/ [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://your-domain.ru/$1/ [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>