htaccess 和Code Igniter中的路由问题

发布于 2024-10-19 07:51:18 字数 1193 浏览 1 评论 0原文

我花了过去 3 个小时试图解决这个问题,但总是有一个部分不起作用,所以如果有人能帮助我,我将不胜感激。

我试图在 htaccess 中实现的目标是: 删除尾部斜杠并强制执行 www.

因此,这 4 个示例需要显示相同的结果:

#1. www.mysite.com/products/show/
#2. www.mysite.com/products/show
#3. mysite.com/products/show/
#4. mysite.com/products/show

它们都需要显示 #2,因为这是正确的。

有很多示例和解决方案可以做到这一点,并且我可以让它发挥作用,但是!我也在这个控制器上使用一条路线,如下所示: $route['产品/(:any)'] = "产品/show/$1";

再一次,htaccess 和路由都工作正常,当 (:any) 包含斜杠时就会出现问题,例如 foo-bar。然后#3 突然失败,网站根本无法加载,并且出于某种奇怪的原因,URL 更改为 www.domain.tld/products/foo-bar (是的,它实际上打印出 domain.tld,我没有将其用作例如,该域当然不是有效的域。)

以下是我目前在 htaccess 中拥有的内容,请记住,为了强制执行 www,我不能!对实际域名进行硬编码,因为将有多个域指向该文档根目录,所有这些域都使用相同的脚本构建。

Options +FollowSymlinks
RewriteEngine On
RewriteBase /



#RewriteRule ^(welcome(/index)?|index(\.php)?)/?$ / [L,R=301]
RewriteCond $1 !^(index\.php|images|doc)
RewriteRule ^(.*)/index/?$ $1 [L,R=301]

# Removes trailing slashes (prevents SEO duplicate content issues)
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ $1 [L,R=301]

# Enforce www

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

感谢您的帮助,

杰森

I have spent the last 3 hours trying to get this right, but there is always one part that doesnt work, so i would be so grateful if someone could help me out.

What im trying to achieve in htaccess is:
To remove trailing slashes, and enforce www.

So these 4 examples need to show the same result:

#1. www.mysite.com/products/show/
#2. www.mysite.com/products/show
#3. mysite.com/products/show/
#4. mysite.com/products/show

They all need to show #2, as that is the correct on.

There are a lot of examples and solutions out there, to do this, and i get it to work, BUT! im also using a route on this controller, looking like this:
$route['products/(:any)'] = "products/show/$1";

Once again, it is all working, both the htaccess and the route, the problem arises when (:any) contains a slash, example foo-bar. Then suddenly #3 fails and the site just doesnt load at all and for some weird reason the url changes to www.domain.tld/products/foo-bar (yes, it actually prints out domain.tld, im not using it as an example. and that domain is of course not even a valid domain.)

Below is what i currently have in my htaccess, please do keep in mind that for the enforce of www, i can not! hard code the actual domain name, because there will be more than one domain pointing to this document root, all using the same script im building.

Options +FollowSymlinks
RewriteEngine On
RewriteBase /



#RewriteRule ^(welcome(/index)?|index(\.php)?)/?$ / [L,R=301]
RewriteCond $1 !^(index\.php|images|doc)
RewriteRule ^(.*)/index/?$ $1 [L,R=301]

# Removes trailing slashes (prevents SEO duplicate content issues)
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ $1 [L,R=301]

# Enforce www

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

Grateful for any help,

Jason

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

遗失的美好 2024-10-26 07:51:18

WWW 问题:也许您需要 mysite.com 的 CNAME 记录才能指向 www.mysite.com?这是我对给定信息的最佳猜测。

URI 路由:请记住,当 /products/(:any) 中的 (:any) 包含斜杠时,路由不再是 /products/(any).. 它是 /products/(any)/(something_else) 。这将作为您的 URL 的另一个段进行处理。

如果斜杠不是路由的一部分,则您将无法在 URL 中使用带有斜杠的内容。如果您只需要在 URL 中传递,您可以执行某种编码/解码。

至于为什么它路由到domain.tld,我不知道。 :)

WWW Problem: Perhaps you need a CNAME record for mysite.com to point to www.mysite.com? That's my best guess with the given information.

URI Routing: Keep in mind, that when (:any) in /products/(:any) contains a slash, the route is no longer /products/(any).. it is /products/(any)/(something_else). This is handled as another segment to your URL.

You won't be able to use something in your URL with a slash in it if the slash is not part of your routing. You could do some kind of encoding/decoding if you just need that passed in the URL.

As to why it's routing to domain.tld, I have no idea. :)

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