的.htaccess重写URL不起作用 [英] .htaccess rewrite url does not work
问题描述
我想重写以下网址:
http://www.domain.com/cars/cars.php?cars_item=231
要
http://www.domain.com/cars/231/
我曾尝试下面的编码,但无法得到它的工作的某些原因。
I have tried the following coding but cannot get it working for some reason.
在子文件夹汽车
我放在.htaccess文件,其中包括:
In sub-folder cars
I placed the .htaccess file which includes:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^([0-9]+)\/?$ cars.php?cars_item=$1 [NC]
</IfModule>
Apache的配置是否正确,因为我已经尝试了重定向从一个域到另一个使用.htaccess文件和工作正常。
Apache is configured properly as I have tried a redirect from one domain to another using an .htaccess file and works fine.
可能有人请向我解释什么,我做错了什么?
Could someone please explain to me what I'm doing wrong?
推荐答案
这个规则应该做你想要的:
This rule should do what you want:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
# Redirect cars.php?cars_item=231 to cars/231/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+cars/cars\.php\?cars_item=([^&\s]+) [NC]
RewriteRule ^ /cars/%1? [R=302,L]
# Internally forward cars/231/ to cars.php?cars_item=231
RewriteRule ^cars/([0-9]+)/?$ /cars/cars.php?cars_item=$1 [NC,L]
第一条规则是负责接待不友好的URL,并将其转换为一个友好的URL。
The first rule is in charge of receiving the unfriendly URL and converting it to a friendly URL.
第二条规则将其重定向到内部不改变浏览器的URL。
The second rule will redirect it internally not changing the browser URL.
这些规则必须被存放在你的情况下,文件夹域的根目录之前,汽车
。
These rules should be placed on the root of your domain in your case the folder before cars
.
您需要做的是这样的,以避免重定向循环,除非你正在使用的httpd的新版本,但因为我不知道任何服务器的细节我只是给你一个普通的例子。
You need to do it like this in order to avoid a redirect loop unless you're using a newer version of HTTPD but since I do not know any server specifics I just gave you a generic example.
如果你想要的是能够访问:
If all you want is to be able to access:
http://www.domain.com/cars/231/
如果没有重定向原始URL,那么你可以只使用:
Without redirecting the original URL then you could just use:
RewriteRule ^cars/([0-9]+)/?$ cars/cars.php?cars_item=$1 [NC,L]
以上规则需要接受上述的URL,并将其发送到正确的处理程序的照顾。
The above rule takes care of receiving the above mentioned URL and send it to the proper handler.
这篇关于的.htaccess重写URL不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!