0

我有这个网址

http://www.mysite.com/product.php?name=Ball&id=25672

我想让它看起来像这样

http://www.mysite.com/product/Ball-25672/

我使用这个.htaccess文件

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{QUERY_STRING} ^name=(.*)&id=(.*)
RewriteRule ^product\.php$ /product/%1-%2/? [L,R=301]

我有两个问题:

1)当我在球项目上滚动鼠标指针时,它显示在浏览器上的链接是第一个,但我希望它显示第二个

2)当我点击 url 时,我得到 404 错误,但浏览器上显示的链接是正确的。

4

1 回答 1

1

First:

When im scrolling the mouse pointer over the ball item the link that it is displayed on the browser is the 1st but i want it to display the 2nd

The rewrite engine will not change the actual content of your site. It only changes request URIs sent by the browser to the server. It's up to you to make your links look like http://www.mysite.com/product/Ball-25672/ and is completely beyond the ability of mod_rewrite.

Second:

When i click on the url i get 404 error but the link displayed on the browser is correct.

You are redirecting the browser to the nicer looking URL: http://www.mysite.com/product/Ball-25672/. The browser then sees this new URL, and requests it from the server. So then the server sees /product/Ball-25672/ and tries to serve it, and it doesn't exist, thus you get a 404. You need 2 set of rules here, one that externally redirects the browser to the nicer looking URL, then one to internally rewrite the nicer looking URL back to where the content actually is:

Options +FollowSymLinks -Multiviews
RewriteEngine on

RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /+product\.php\?name=([^&]+)&id=([0-9]+)
RewriteRule ^ /product/%2-%3/? [L,R=301]

RewriteRule ^product/(.+)-([0-9]+)/?$ /product.php?name=$1&id=$2 [L]
于 2013-11-12T15:32:43.297 回答