大网数据广告活动
服务器运维问答

apache2 Path rewrite to Get Parameter - Make Slashes work

· 52CE 我爱测

The following is my current .htaccess file. If I open https://example.com/test and it doesn't end with one of the endings specified in line 2, it will open https://example.com/index.php?url=test RewriteEngine on RewriteCond %{REQ…

问题原文

原帖链接 · OfficialCRUGG · CC BY-SA 4.0

The following is my current .htaccess file. If I open https://example.com/test and it doesn't end with one of the endings specified in line 2, it will open https://example.com/index.php?url=test

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !\.(css|js|png|jpg|php|svg|gif|webm|webp|eot|ttf|woff|woff2)
RewriteRule ^([^/]+)/? index.php?url=$1 [L,QSA]

However, if I try to open https://example.com/something/test, it just opens ?url=something instead of ?url=something/test. How can I achieve this? I have absolutely no knowledge about regex stuff

已采纳解答

原帖链接 · DocRoot · CC BY-SA 4.0

The regex ^([^/]+)/? only captures the first path segment, ie. [^/]+ matches 1 or more non-slash characters. If you wish to match the entire URL-path then use (.+) (to match 1 or more any characters). However, you can be more restrictive if you only want to match say 1 or 2 path segments?

and it doesn't end with one of the endings specified in line 2

That condition doesn't specifically check that the URL "ends" with one of those extensions, it checks that one of those extensions are contained within the filesystem path that the URL maps to. You need an end-of-string anchor $ at the end of the regex. And to specifically check the URL, you should check against the REQUEST_URI server variable.

So, this becomes:

RewriteEngine on
RewriteCond %{REQUEST_URI} !\.(css|js|png|jpg|php|svg|gif|webm|webp|eot|ttf|woff|woff2)$
RewriteRule (.+) index.php?url=$1 [L,QSA]

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2019-10-13)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。