1

我正在使用此规则来重写链接

RewriteEngine On
RewriteRule ^(.*)/(.*) show_cv.php?email=$1

它工作正常,就像我用最后一个斜杠写这个 url

www.mysite.com/letschat_2008@yahoo.com/  ---->  index.php?email=letschat_2008@yahoo.com

但是当我从链接中删除最后一个斜杠时,www.mysite.com/letschat_2008@yahoo.com/它会显示错误 404。

我希望 URL 重写规则适用于带有斜线和不带斜线 ( /)

www.mysite.com/letschat_2008@yahoo.com/ ----> index.php?email=letschat_2008@yahoo.com
www.mysite.com/letschat_2008@yahoo.com ----> index.php?email=letschat_2008@yahoo.com
4

3 回答 3

1

您的规则正在循环,您需要确保您正在重写电子邮件地址,并添加一些条件,以便在访问现有资源时不会应用该规则:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([A-Za-z0-9_\-\@\.]+)/?$ /show_cv.php?email=$1 [L]
于 2012-07-31T17:44:07.057 回答
0

然后,您应该使用以下规则:

RewriteRule ^(.*)/? show_cv.php?email=$1
于 2012-07-31T16:41:23.770 回答
0

I assume you note these rules in a .htaccess file, not in the server configuration when looking at your description ?

Rethink if you don-t want to put this into the server configuration. Apart from the usage of .htaccess files being harder to debug using rewrite rules in those files is more complex than in the server configuration. This is documented in mod_rewrites docs.

The reason for the behaviour is the different content of REQUEST_URI in both cases. Have a try checking this directly and you will see the problem. The whole part "letschat_2008@yahoo.com" is simply missing in that variable in case 2 (no "/"). To get this working you must use an additional rewriteCondition (also documented...). Something like these untested lines:

RewriteEngine On
RewriteCond %{REQUEST_URI} ^/(.+)&
RewriteRule - show_cv.php?email=%1

(note the '%' instead of a '$' in the last line)

于 2012-07-31T16:44:54.370 回答