如何将多个 URL 重定向到主页的单个 URL?

发布于 2024-09-19 08:22:58 字数 719 浏览 5 评论 0原文

我有一个基于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

多孤肩上扛 2024-09-26 08:22:59

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.

趁微风不噪 2024-09-26 08:22:59

您当前的解决方案不起作用,因为任何输入都已被 RewriteRule ^(.*)$ /index.php/$1 [QSA,L] L 标志表示最后,意味着不应处理进一步的规则。

Options +FollowSymLinks
RewriteEngine on
RewriteBase /

#Everything from here has to do with the line mentioned above
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteCond $1 !^(index\.php|images|css|js|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [QSA,L] # This catches all input

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

RewriteRule ^en(/home.html)?$ /en/ [L]

相反,您想要处理的任何内容都需要移至该行之前:

Options +FollowSymLinks
RewriteEngine on
RewriteBase /


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

RewriteRule ^en(/home.html)?$ /en/ [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteCond $1 !^(index\.php|images|css|js|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [QSA,L]

除此之外,请阅读 mod_rewrite正则表达式 在这里不会有什么坏处。

Your current solution doesnt work because any input has allready been captured by RewriteRule ^(.*)$ /index.php/$1 [QSA,L] The L flag denotes last, meaning that no further rules should be processed.

Options +FollowSymLinks
RewriteEngine on
RewriteBase /

#Everything from here has to do with the line mentioned above
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteCond $1 !^(index\.php|images|css|js|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [QSA,L] # This catches all input

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

RewriteRule ^en(/home.html)?$ /en/ [L]

Instead, anything you want processed needs to be moved up before that line:

Options +FollowSymLinks
RewriteEngine on
RewriteBase /


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

RewriteRule ^en(/home.html)?$ /en/ [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteCond $1 !^(index\.php|images|css|js|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [QSA,L]

Beyond that, reading up on mod_rewrite and regular expressions cant hurt here.

维持三分热 2024-09-26 08:22:59

就像将

^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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文