【问题标题】:Apache 2.2 mod_rewrite helpApache 2.2 mod_rewrite 帮助
【发布时间】:2011-06-02 04:20:41
【问题描述】:

我正在尝试让 Apache 2.2 mod_rewrite 获得干净的 url。 我有诸如

之类的链接
<ul>
<li><a href="index.php?view=pageName">Page Name</a></li>
<li><a href="index.php?view=pageName2">Page Name2</a></li>
<li><a href="index.php?view=pageName3">Page Name3</a></li>
</ul>

然后 url 出来了

http://example.com/user/index.php?view=pageName

我想把地址栏中的url清理成这个

http://example.com/user/pageName

编辑:如果有用的话,这就是我的 httpd.conf 中的内容。

<Directory "C:/Apache2.2/htdocs/user">
    Options Indexes FollowSymLinks
    AllowOverride all
    order allow,deny
    Allow from all
</Directory>

使用 phpinfo() 我已验证 mod_rewrite 已加载,并且我的 .htaccess(用户文件,不是 root .htaccess)中有这个,而不是 apache 上的虚拟用户

RewriteEngine on
RewriteBase /user/ #Edited rewrite base added in, but not helping much.
RewriteOptions Inherit
RewriteRule ^/user/([a-zA-Z])/?$ index.php?view=$1  [NC,L] 

编辑:我的 .htaccess 的其余部分

#Ensure browser reads Header
Header unset ETag
FileETag None
Header unset Last-Modified

#Set caching expires
Header set Expires On
ExpiresDefault "access plus 30 days"

#gzip
<FilesMatch "\.(js|css)$">
SetOutputFilter DEFLATE
</FilesMatch>

在所有 html 结构相同的菜单链接上的工作方式不同,并且无法正常工作。 IE:它将我发送到http://example.com/user/?view=pageName,但正在加载根 HTML 文件内容。

如果我改变这一行

RewriteRule ^/user/([a-zA-Z])/?$ index.php?view=$1  [NC,L]

RewriteRule ^index\.php$ http://www.google.com  [NC,L]

我按预期被发送到谷歌。所以很明显我在匹配和替换方面做错了什么,但我做错了什么? 提前致谢。

编辑: access.log 和 error.log 没有错误。

【问题讨论】:

    标签: php apache mod-rewrite clean-urls


    【解决方案1】:
    RewriteRule ^/user/([a-zA-Z]+)?$ index.php?view=$1  [NC,L]
    

    你缺少 + 所以你没有匹配单词,只是一个字符

    【讨论】:

    • @robx 抱歉,我曾经检查过我的规则(带 +),但在发布时我使用了你的规则,那时我忘记了“+”...
    • 我也试过加号,这里没有任何改变。没有干净的 url,但我可以使用 R=301 类型并在基本 url /user 被命中时重定向到其他域,如 google。
    • 也许是其他原因造成的,但我不知道它可能是什么,因为我可以重定向到其他域。这可能是我的网址在 php 中的结构方式吗?
    【解决方案2】:
    RewriteRule ^/user/([a-zA-Z])?$ index.php?view=$1  [NC,L] 
    

    试试这个..(删除一个斜线)

    【讨论】:

    • 不工作。奇怪的是,我有几个 ul 列表,每个 ul 的最后一个 li 链接默认为 example.com/index.php?view=pageName,而其余的则指向 example.com/?view=pageName。这真的很奇怪吗?
    • .htaccess 中此规则之前或之后的任何其他规则?
    • 是的,但不认为这会影响模组重写。我将编辑并发布我的整个小 htaccess。
    • 我尝试在我的服务器上删除 [NC,L] 它在这里工作。发布您的 .htaccess 以提供更多帮助。
    • 我确实发布了编辑并发布了它。那是我的整个 .htaccess,我没有根 .htaccess 文件。删除 [NC,L] 对我没有任何作用。没有错误,并且允许我重定向到其他网站,但实际上并没有清理我的网址。
    【解决方案3】:

    已修复:必须更改 HTML URL 样式才能使用

    <a href="pageName">Page Name</a>
    

    然后在.htaccess

    RewriteEngine On
    RewriteOptions Inherit
    ReWriteBase /user/
    RewriteRule ^([a-zA-Z0-9]+)/?$ index.php?view=$1 [QSA,L]
    

    还必须从 apache 的 httpd.conf 中禁用 mod_cache.so,因为如果留下任何缓存,它会干扰重写。希望这可以帮助其他人获得干净的网址。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-07-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多