简体   繁体   English

在本地主机中使用.htaccess文件重写URL

[英]Rewrite urls with .htaccess file in localhost

So I have managed to rewrite urls using the .htaccess file but the redirection messes up the url. 因此,我设法使用.htaccess文件重写了url,但是重定向弄乱了该url。 For example I try to rewrite this: 例如,我尝试重写此代码:

localhost/mysite/public_html/class/5/

into this: 到这个:

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

and I use the following lines in the .htaccess file: 我在.htaccess文件中使用以下几行:

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

but the browser redirects me here (I am using wamp on Windows 7): 但是浏览器将我重定向到此处(我在Windows 7上使用wamp ):

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

Which is wrong and gets me a 403 error. 这是错误的,并导致我出现403错误。 What should I do? 我该怎么办?

The problem must come from the ./ in your replacement function (./public_html/index.php). 该问题必须来自替换功能(./public_html/index.php)中的./。 A rewrite rule matches the regex you place between ^ and $ and then replaces ONLY that with your second part. 重写规则匹配您放置在^和$之间的正则表达式,然后仅将其替换为第二部分。 I'm assuming you already have some sort of rewritecond before the rule, so try removing the "./" 我假设您在规则之前已经有了某种rewritecond,因此请尝试删除“ ./”

Try changing your rewritecond to: 尝试将您的rewritecond更改为:

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

Which means that if the requested URL is not a file or directory, then it will follow through and try your rewriterule 这意味着,如果所请求的URL不是文件或目录,那么它将继续进行并尝试重写

您的规则应如下所示:

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

I don't know which rules you've got before, but your minimal htaccess should be this one : 我不知道您以前有哪些规则,但是您的最小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]

Avoid using ./ in your rules, and don't forget the location path must include your mysite folder 避免在规则中使用./ ,并且不要忘记位置路径必须包含mysite文件夹

It's unbelievable. 这太不可思议了。 The error was a result of Firefox's caching. 该错误是Firefox缓存的结果。 I used Chrome and it was ok. 我使用了Chrome,还可以。 So I cleared Firefox's cache and everything worked fine. 因此,我清除了Firefox的缓存,一切正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM