【问题标题】:Rewrite urls with .htaccess file in localhost在 localhost 中使用 .htaccess 文件重写 url
【发布时间】:2012-07-26 22:01:34
【问题描述】:

所以我已经设法使用 .htaccess 文件重写了 url,但是重定向弄乱了 url。例如我尝试重写这个:

localhost/mysite/public_html/class/5/

进入这个:

localhost/mysite/public_html/index.php?v=class&id=5

我在 .htaccess 文件中使用以下行:

RewriteRule ^public_html/([A-Za-z]+)/([0-9]+)/?$ ./public_html/index.php?v=$1&id=$2 [L,R]

但浏览器将我重定向到此处(我在 Windows 7 上使用 wamp):

localhost/C:/wamp/www/mysite/public_html/index.php?v=class&id=5

这是错误的,让我收到 403 错误。我该怎么办?

【问题讨论】:

  • 自从wamp上有一个public_html?
  • 我为结构目的创建了这个文件夹。

标签: php apache .htaccess url mod-rewrite


【解决方案1】:

您的规则需要如下所示:

RewriteRule ^public_html/([A-Za-z]+)/([0-9]+)/?$ /public_html/index.php?v=$1&id=$2 [L,R]

【讨论】:

    【解决方案2】:

    问题必须来自替换函数中的 ./ (./public_html/index.php)。重写规则匹配您放置在 ^ 和 $ 之间的正则表达式,然后仅用您的第二部分替换它。我假设您在规则之前已经有某种 rewritecond,所以请尝试删除“./”

    尝试将您的 rewritecond 更改为:

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    

    这意味着如果请求的 URL 不是文件或目录,那么它将继续执行并尝试您的重写器

    【讨论】:

    • 删除“./”对我没有用。作为一个 rewritecond 我有这个: RewriteCond %{HTTP_HOST} !^localhost/mysite/$ [NC] 我不确定它是否正确。
    • 我正在打电话,但请确保那些 rewritecond 在您的重写规则之前位于不同的行上。还要确保你有 rewriteengine 和 mod rewrite 启用
    • 我尝试过使用大量不同的重写条件但没有成功。似乎没有其他人有这个问题,所以我找不到任何解决方案。我已经连续两天试图找出原因。无论如何感谢您的帮助!
    • 您是否已将 httpd.config 文件从“AllowOverride None”更改为“AllowOverride All”?它将在 标签内
    【解决方案3】:

    我不知道你以前有哪些规则,但你的最小 htaccess 应该是这个:

    RewriteEngine On
    RewriteBase /mysite/
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    
    RewriteRule ^public_html/([A-Za-z]+)/([0-9]+)/?$ /mysite/public_html/index.php?v=$1&id=$2 [L,R]
    

    避免在您的规则中使用./,并且不要忘记位置路径必须包含您的mysite 文件夹

    【讨论】:

      【解决方案4】:

      难以置信。该错误是 Firefox 缓存的结果。我用了 Chrome,没问题。所以我清除了 Firefox 的缓存,一切正常。

      【讨论】:

        猜你喜欢
        • 2018-10-18
        • 1970-01-01
        • 2023-03-03
        • 2013-04-22
        • 2017-04-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多