.htaccess url重写删除字符串



我有以下格式的URL:

http://localhost/profile.php?name=billgates

我使用了以下.htaccess文件

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?profile/(.*?)/?$ /profile.php?name=$1 [L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9} /profile.php?name=([^& ]+)
RewriteRule ^/?profile.php$ /profile/%1? [L,R=301]

它将url转换为以下格式:

http://localhost/profile/billgates

请告诉我如何编辑.htaces文件以删除配置文件/并以格式创建URL

http://localhost/billgates

为了避免冲突,您需要对正则表达式进行尽可能严格的限制。例如,您需要能够区分/<some-name>/<some-page>(如果这是您网站上的一件事(。

根据您的示例,我假设名称只能由小写字母组成。

您还应该在内部重写之前进行外部重定向

请尝试以下操作:

Options -MultiViews
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9} /profile.php?name=([^& ]+)
RewriteRule ^profile.php$ /%1 [QSD,R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z]+?)/?$ /profile.php?name=$1 [L]

由于regex现在限制性更强(只有小写字母(,可以删除检查请求与文件不匹配的文件系统检查(文档根中的文件不太可能只由小写字母组成(。

最新更新