.htaccess - RewriteRules 正常工作,但浏览器地址栏显示完整(不友好)的 URL

发布于 2024-08-28 01:29:44 字数 1318 浏览 3 评论 0原文

无法在网络或这些论坛上找到此问题的解决方案 - 如果我错过了某些内容,请道歉!

我的 .htaccess RewriteRules 运行良好 - 我的网页中有搜索引擎和用户友好的链接,并且隐藏在后台运行的不友好的数据库 URL。

除非我添加了 RewriteRule 来添加“www”。如果用户没有输入 URL,则将其放在 URL 的前面 - 以确保只有一个出现在搜索引擎中。这就是现在发生的事情,我不明白为什么!

我的友好 URL 结构是这样的,数据库查询字符串使用第一个“重要词”:

www.example.com/importantword-nonimportantword/

.htaccess snippet:

Options +FollowSymLinks
Options -Indexes

RewriteEngine on
RewriteOptions MaxRedirects=10

RewriteBase /

RewriteRule ^/$ index.php [L]

RewriteRule ^(.*)-(.*)/overview/$ detail.php?categoryID=$1 [L]

RewriteCond %{HTTP_HOST} !^www.example.com$
RewriteRule ^(.*)$ http://www.example.com/$1 [L]

自从我添加最后 2 行以来发生了什么:

案例 1:用户输入(或点击)www。 example.com/honda-vehicle/overview/
- 工作正常
- 他们被带到正确的页面,浏览器 URL 栏显示:

www.example.com/honda-vehicles/overview/

案例 2:用户类型 example.com
- 工作正常
- 它们被带到 www.example.com,浏览器 URL 栏显示:

www.example.com

情况 3:用户键入(或点击)example.com/honda-vehicles/overview/ 即不带前缀“www”
- 无法正常工作
- 它们被带到正确的页面,但浏览器 URL 栏显示不友好的 URL:

www.example.com/detail.php?categoryID=honda 

我认为 RewriteRules 的顺序存在一些问题,但它正在努力尝试逻辑地逐步通过它并找出答案!

任何帮助或指示将不胜感激!

Haven't been able to find a solution to this around the net or these forums - apologise if I've missed something!

My .htaccess RewriteRules are working well - have search-engine and user -friendly links in my web pages, and unfriendly database URLs running hidden in the background.

Except when I added a RewriteRule to add "www." to the front of URLs if the user didn't enter it - to ensure only one appears in search engines. Here's what now happening, and I can't figure out why!

My friendly URL structure for content is like this, and the database query string uses the first "importantword":

www.example.com/importantword-nonimportantword/

.htaccess snippet:

Options +FollowSymLinks
Options -Indexes

RewriteEngine on
RewriteOptions MaxRedirects=10

RewriteBase /

RewriteRule ^/$ index.php [L]

RewriteRule ^(.*)-(.*)/overview/$ detail.php?categoryID=$1 [L]

RewriteCond %{HTTP_HOST} !^www.example.com$
RewriteRule ^(.*)$ http://www.example.com/$1 [L]

What's happening since I added the last 2 lines:

CASE 1: user types (or clicks) www.example.com/honda-vehicle/overview/
- Works correctly
- They are taken to the correct page and the browser URL bar says:

www.example.com/honda-vehicles/overview/

CASE 2: user types example.com
- Works correctly
- They are taken to www.example.com and the browser URL bar says:

www.example.com

CASE 3: user types (or clicks) example.com/honda-vehicles/overview/ i.e. without the prefix "www"
- Does NOT work correctly
- They are taken to the right page, but the browser URL bar displays the unfriendly URL:

www.example.com/detail.php?categoryID=honda 

I figure there's some issue with the order of the RewriteRules, but it's doing my head in trying to logically step through it and figure it out!

Any assistance at all or pointers would be most appreciated!

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

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

发布评论

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

评论(1

太阳哥哥 2024-09-04 01:29:44

规则按其出现的顺序应用。这意味着,当请求 honda-vehicles/overview/ 时,将应用第二条规则,重写 detail.php?categoryID=honda 的路径。由于 L 标志,当前的重写过程会立即停止,但也会使用新的 URL 重新启动,导致现在应用第三条规则。

您只需更改顺序即可解决此问题。将最后一条规则作为第一条规则(并添加 R 标志):

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

经验法则是将导致外部重定向的规则放在仅导致内部重写的规则之前。

Rules are applied in the order they appear. That means, when honda-vehicles/overview/ is requested, your second rule is applied, rewriting the path to detail.php?categoryID=honda. And due to the L flag, the current rewrite process is stopped immediately but also restarted with the new URL, causing that now the third rule is applied.

You can fix this by simply changing the order. Put your last rule as first rule (and add the R flag):

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

The rule of thumb is to put those rules, that cause an external redirect, in front of those rules, that just cause an internal rewrite.

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