1

I have a mod_rewrite rule working to direct non-existing directories to a php file which does a database get based on the $1.

Everything works fine unless the directory does exist, which displays the proper directory, but it also appends the query string when it's not necessary.

I have been scouring the web for the past few hours and trying different methods with no luck.

Does anyone know how to keep this functioning as-is, but get rid of the query string?

Thanks much.

Here is my code:

RewriteEngine On
    Options +FollowSymlinks
    RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^([0-9a-zA-Z\-]+)/$ $1 [R]
RewriteRule ^([0-9a-zA-Z\-]+)$ product.php?product=$1

What ends up happening is the browser displays the URL as http://domain.com/existing_dir/?product=existing_dir

4

2 回答 2

2

试试看,它会/自行删除而不重复整个过程

RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*?)/?$ product.php?product=$1

如果你坚持限制特殊字符,那会:

RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^([0-9a-zA-Z\-]+?)/?$ product.php?product=$1

+是 1 次或更多重复,*是 0 次或更多,并且+?,*?是不饥饿匹配的修饰符 - 它允许/?匹配任何内容

此外,在您的示例中,第一个 RewriteRule 有条件地执行(当目录不存在时),第二个总是执行(如果第一个不破坏进程),所以即使目录存在

于 2012-05-10T06:59:24.290 回答
0

Mod_rewrite 不会影响查询字符串,它将始终被标记到新 URL 的末尾,除非您告诉 mod_rewrite 有一个空的查询字符串。这是通过添加一个 ? 在新字符串的末尾。所以第一行应该是这样的:

RewriteRule ^([0-9a-zA-Z\-]+)/$ $1? [R]
于 2012-05-10T07:39:25.503 回答