大网数据广告活动
网站运营与故障问答

.htaccess - RewriteRule for 404 and query

· 52CE 我爱测

I am very new to this stuff. I have a website with these RewriteCond / RewriteRule directives. Options All -Indexes RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FI…

问题原文

原帖链接 · 3rKaN_BRATTE · CC BY-SA 4.0

I am very new to this stuff. I have a website with these RewriteCond/RewriteRule directives.

Options All -Indexes

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l

RewriteRule ^(.+)$ index.php?page=$1 [QSA,L]

So far everything works on my website but I have a small issue. I will try to explain it.

  • https://example.com/login [Shows the login page]

  • https://example.com/asdasdasd [Shows the 404 not found page]

  • https://example.com/login/asdasdasd [Shows the login page too but it should show 404 not found page]

  • https://example.com/login%F0%9F%91%88%F0%9F%91%88%F0%9F%91%88 [Shows the login page too but it should show 404 not found page]

and I would like that every Query parameter will be removed.

If I open https://example.com/login?fbclid=IwAR2w6Ff that it redirects back to https://example.com/login

已采纳解答

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

It would seem "the problem" is in your PHP code, not .htaccess as you seem to suggest.

RewriteRule ^(.+)$ index.php?page=$1 [QSA,L]

All requests are rewritten to index.php (your "front-controller") which then routes your URLs. It is this PHP script that determines whether /login or /login/asdasdasd, etc. resolve to a valid page on your site. This has nothing to do with Apache or your .htaccess file.

Aside: If there was a physical file at /login.php and MultiViews was enabled then this could be a different matter. However, it does not appear that MultiViews is enabled (ie. Options All -Indexes) so this does not seem to apply, even if login.php does exist as a physical file.

and i would like that every Query parameter will be removed.

if i open https://example.com/login?fbclid=IwAR2w6Ff that it redirects back to https://example.com/login

However, this you can do in .htaccess. At the top of your .htaccess file:

RewriteCond %{QUERY_STRING} .
RewriteRule ^(login)$ /$1 [QSD,R=301,L]

To remove the query string from any URL, not just /login then change the above RewriteRule to read:

RewriteRule (.*) /$1 [QSD,R=301,L]

Test with 302 (temporary) redirects first to avoid caching issues.

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

资料来源与适用范围

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

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

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