如何将多个 URL 重定向到主页的单个 URL?
我有一个基于 codeigniter 构建的网站,但遇到了一个小问题。基本上可以通过 3 个不同的 url 访问主页,即:
www.domain.com/en
www.domain.com/en/
www.domain.com/en/home.html
我希望第一个和第三个 url 进行重定向到第二个(www.domain.com/en/)。
我已经使用 .htaccess 几个小时了,但似乎无法完成任何更改。
任何帮助将不胜感激。
编辑: 这是我当前的 .htaccess
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|images|css|js|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [QSA,L]
RewriteCond %{HTTP_HOST} ^mywebsite.com [NC]
RewriteRule ^(.*)$ http://www.mywebsite.com/$1 [L,R=301]
RewriteRule ^en(/home.html)?$ /en/ [L]
I have a site built on codeigniter but I have come accross a small issue. Basically the homepage can be accessed through 3 different urls, being:
www.domain.com/en
www.domain.com/en/
www.domain.com/en/home.html
I'd like the first and third url to redirect to the second (www.domain.com/en/).
I've been playing around with .htaccess for hours and I can't seem to get any changes done.
Any help would be really appreciated.
Edit:
Here is my current .htaccess
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|images|css|js|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [QSA,L]
RewriteCond %{HTTP_HOST} ^mywebsite.com [NC]
RewriteRule ^(.*)$ http://www.mywebsite.com/$1 [L,R=301]
RewriteRule ^en(/home.html)?$ /en/ [L]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
RewriteRule ^en(/home.html)?$ /en/ [L]
这只匹配 /en 和 /en/home.html,不匹配其他 url。
RewriteRule ^en(/home.html)?$ /en/ [L]
This matches only /en and /en/home.html, no other urls.
您当前的解决方案不起作用,因为任何输入都已被
RewriteRule ^(.*)$ /index.php/$1 [QSA,L]
L
标志表示最后,意味着不应处理进一步的规则。相反,您想要处理的任何内容都需要移至该行之前:
除此之外,请阅读 mod_rewrite 和 正则表达式 在这里不会有什么坏处。
Your current solution doesnt work because any input has allready been captured by
RewriteRule ^(.*)$ /index.php/$1 [QSA,L]
TheL
flag denotes last, meaning that no further rules should be processed.Instead, anything you want processed needs to be moved up before that line:
Beyond that, reading up on mod_rewrite and regular expressions cant hurt here.
就像将
^en(/home.html)?$ /en/ [L]
更改为
^/en(/home.html)?$ /en/ [L] 一样简单
(注意额外的斜杠)Could be as simple as changing
^en(/home.html)?$ /en/ [L]
to
^/en(/home.html)?$ /en/ [L]
(notice the extra slash)