我想将所有用户从mysite.com/username
重定向到mysite.com/user.php?user=username
我现在使用以下代码:
RewriteEngine on
RewriteBase /
RewriteRule ([^/.]+)/ user.php?user=$1 [L]
RewriteRule ([^/.]+) user.php?user=$1 [L]
虽然有效,但它也将目录路径重定向到user.php
(例如mysite.com/css/style.css
- > mysite.com/user.php?user=css/style.css
)。
我该怎么办?有什么建议吗?
答案 0 :(得分:1)
您只需要跳过对这样的真实文件和目录的请求:
RewriteEngine on
RewriteBase /
# If the request is not for a valid directory
RewriteCond %{REQUEST_FILENAME} !-d
# If the request is not for a valid file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ([^/.]+)/?$ user.php?user=$1 [L,QSA]
答案 1 :(得分:0)
添加RewriteCond
以排除以.css
结尾的请求:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} !\.css$
RewriteRule ([^/.]+)/? user.php?user=$1 [L]
我使用问号?
将您的2条规则合并为一条,以使结尾斜线可选。
如果您愿意,还可以排除其他“帮助”文件。并且排除重新写入资源请求 user.php 可能会有所帮助:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} !\.(css|js|png|jpe?g|gif)$
RewriteCond %{REQUEST_URI} !user.php$
RewriteRule ([^/.]+)/? user.php?user=$1 [L]