htaccess 添加尾部斜杠并强制 www 带有干净的 url
我正在使用带有 mod_rewrite 的 htaccess 文件来创建这样的干净 url:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
</IfModule>
我还想强制该网站具有“www”子域,并且最重要的是,如果该 url 没有,则添加一个尾部斜杠。
我是一个绝对的 mod_rewrite 菜鸟,我尝试通过结合我在谷歌上找到的其他代码来自己完成这个任务(我知道很遗憾),但我总是以 500 错误告终。
这是我找到的强制 www 的代码:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteCond %{HTTP_HOST} ^www\.domain\.tld$ [NC]
RewriteRule ^(.*)$ http://domain.tld/$1 [R=301,L]
</IfModule>
感谢您的帮助。
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>
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.
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.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试将 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.
To debug, comment out the individual sections and see what is/isn't working.
使用这个并忘记你的问题;)
Use this and forgot your problems ;)