【发布时间】:2018-06-22 21:47:48
【问题描述】:
目前我有以下.htaccess 文件:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME}% !-d
RewriteCond %{REQUEST_FILENAME}% !-f
RewriteRule \.(js|css)$ - [L]
RewriteRule ^/?u/(.*?)/?$ /user-profile?user_id=$1 [L]
RewriteRule ^(.+)$ index.php [QSA,L]
它将所有文件和目录重写为index.php,它会进一步路由,忽略静态js/css文件。
用这一行:
RewriteRule ^/?u/(.*?)/?$ /user-profile?user_id=$1 [L]
我将所有请求重定向到 website.com/user-profile?user_id=timm 到 website.com/u/timm 之类的东西。我试图弄清楚如何让它重定向到简单的website.com/timm,但到目前为止我尝试过的一切都给了我一个 500 错误。
【问题讨论】:
-
"用这一行...我正在重定向..." - 不,你从左到右重写,而不是反过来。
-
@smith 如果我删除该行,我将无法再使用静态资源。
-
@Jeff 你有解决办法吗?
-
RewriteRule ^(timm)?$ /user-profile?user_id=$1 [L]应该可以解决问题(但我现在无法测试) -
如果你只是想要一个重写规则将
website.com/u/timm重定向到website.com/timm,试试RewriteRule ^\/u\/(\w+)$ /$1
标签: php apache .htaccess url-rewriting routing