【问题标题】:How do I get mod-rewrite to access the proper directory?如何让 mod-rewrite 访问正确的目录?
【发布时间】:2014-06-24 14:44:14
【问题描述】:

我正在尝试使用 mod-rewrite 来消除像 {root}/index.php?action=about&page=faq 这样的网址。我希望 {root}/about/faq 重定向到 php url。我的 .htaccess 文件看起来像

DirectoryIndex index.php
RewriteEngine On
RewriteRule ^(index|about|calendar|news|contact)$ index.php?action=$1 [L]
RewriteRule ^(index|about)/(.+)$ index.php?action=$1&page=$2 [L]

当重定向到 index.php?action=$1 时,这工作正常。但是,当使用第二条规则时,它并不能完全发挥作用。 html 内容看起来不错,但缺少样式表和图像。该页面似乎认为它位于子目录中。因此 {root}/about/faq 在 about/images 文件夹中查找图像,而不是在根目录下的 images 文件夹中查找。然而,PHP 脚本说工作目录仍然是根目录。如何让页面认为它们位于根目录中?谢谢!

【问题讨论】:

    标签: php apache .htaccess mod-rewrite


    【解决方案1】:
        RewriteRule ^(index|about|calendar|news|contact)/(.*)/$ /index.php?action=$1&page=$2
    

    会导致 /about/faq/ 重写为 index.php?action=about&page=faq

    /calendar/month/ 将导致 index.php?action=calendar&page=month

    除非你想重写我会添加的图像文件:

        RewriteCond %{REQUEST_URI} !(\.gif|\.jpg|\.png)$ [NC]
    

    这将停止重写图像请求。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-18
      • 1970-01-01
      • 1970-01-01
      • 2016-10-19
      • 1970-01-01
      相关资源
      最近更新 更多