【问题标题】:Changing url with RewriteRule does not replace the url and redirects my page使用 RewriteRule 更改 url 不会替换 url 并重定向我的页面
【发布时间】:2013-06-21 19:30:36
【问题描述】:

我正在尝试使用以下 mod_rewrite 代码将我的网站的 url 更改为不同的,但它似乎正在重定向并且我希望页面加载我的目标 url。

假设我要加载网址:www.AAAAAA.com/store/index.php?categ=cars 但在 URL 栏中,我想显示以下内容: www.AAAAAA.com/store/cars

我的代码是这样的(但由于它重定向并且无法正确重写而无法正常工作):

RewriteEngine On
RewriteRule ^store/(.*)$ store/index.php?categ=$1 [L]

感谢您的任何意见!

【问题讨论】:

  • mod_rewrite 重写传入的请求,它不适应您的 html 中的现有链接。那是你的工作。
  • @mario 我不是在尝试调整任何现有链接,只是为了重写向用户显示的 URL,就像许多网站一样
  • 那么,呃,您是否更改了 your html 以包含新的漂亮 url?
  • mm nop。我应该在那里做什么样的改变?它是根据 'categ' 参数动态加载的 php

标签: php apache .htaccess url mod-rewrite


【解决方案1】:

试试这个代码:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(store)/index\.php\?categ=([^\s]+) [NC]
RewriteRule ^ /%1/%2? [R=302,L]

RewriteCond %{REQUEST_FILENAME} !-f    
RewriteRule ^store/(.+?)/?$ /store/index.php?categ=$1 [L,QSA,NC]

一旦您确认它工作正常,请将R=302 替换为R=301。在测试你的 mod_rewrite 规则时避免使用R=301(永久重定向)。

【讨论】:

  • 您尝试测试什么 URI?这个 .htaccess 在你的路径中放置在哪里?
  • 'ABCD.com/store/index.php?categ=headbands' 我将 htaccess 放在主目录中。但我仍然在顶部栏中看到相同的 URL
  • 抱歉,在我的代码中存储之前有一个额外的悬空斜线。刚刚编辑,现在试试。
  • 这个很好用。你介意快速解释一下什么规则在做什么吗?我正在尝试为另一个页面遵循相同的模型...
  • 第一条规则用于外部重定向。 %{THE_REQUEST} 表示 Apache 收到的原始请求。在该行中,我在%1 中捕获文字字符串store,在%2 中捕获categ GET 参数(headbands)的值。然后使用 RewriteRule 我重定向到/store/headbands? 最后用于从 URL 中剥离查询字符串。
【解决方案2】:

试试这个:

Options -Multiviews
RewriteEngine on
RewriteRule ^(store/images|store/css|store/js)($|/) - [L]
RewriteBase /store/
RewriteRule ^store/(.*)$  index.php?categ=$1 [QSA,L]

您可以使用第 3 行从重写中排除任何目录,例如您不希望重写 domain.com/store/images/logo.jpg 的请求。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-02-24
    • 2019-03-09
    • 1970-01-01
    • 2012-12-15
    • 2014-01-23
    • 2013-10-12
    • 2018-06-16
    相关资源
    最近更新 更多