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

Why is this .htaccess evaluating to 302 redirect instead of internal rewrite?

· 52CE 我爱测

As an example, I was expecting this https://example.software/credits to evaluate to an internal rewrite https://example.software?mode=credits Why is it showing up on the browser url-bar as a 302 redirect? RewriteEngine On #Put in…

问题原文

原帖链接 · Rohit Gupta · CC BY-SA 4.0

As an example, I was expecting this

https://example.software/credits

to evaluate to an internal rewrite

https://example.software?mode=credits

Why is it showing up on the browser url-bar as a 302 redirect?

RewriteEngine On
#Put in by the hosting company to enforce https
RewriteCond %{HTTP_HOST} example\.software [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://example.software/$1 [R,L]

#Bunch of domains, not being used yet
RewriteCond %{HTTP_HOST} ^example\.app$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www\.example\.app$ [NC,OR]
RewriteCond %{HTTP_HOST} ^example\.kiwi$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www\.example\.kiwi$ [NC,OR]
RewriteCond %{HTTP_HOST} ^example\.nz$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www\.example\.nz$ [NC,OR]
RewriteCond %{HTTP_HOST} ^example\.online$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www\.example\.online$ [NC]
RewriteRule ^ https://example.software/$1 [R=302,L]

#Internal redirects
RewriteCond %{HTTP_HOST} ^example.software$ [NC]
RewriteRule ^home$ https://example.software\?mode\=home&%{QUERY_STRING}  [L]
RewriteRule ^contacts$ https://example.software\?mode\=contacts&%{QUERY_STRING}  [L]
RewriteRule ^credits$ https://example.software\?mode\=credits&%{QUERY_STRING}  [L]
RewriteRule ^downloads$ https://example.software\?mode\=downloads&%{QUERY_STRING}  [L]
RewriteRule ^developer$ https://example.software\?mode\=developer&%{QUERY_STRING}  [L]
RewriteRule ^projects$ https://example.software\?mode\=projects&%{QUERY_STRING}  [L]
RewriteRule ^(download)/([^/]+)$ https://example.software?mode=$1&name=$2 [L]
RewriteRule ^(code)/([^/]+)$ https://example.software?mode=$1&name=$2 [L]
RewriteRule ^(payment)/([^/]+)$ https://example.software?mode=$1&name=$2 [L]
RewriteRule ^(donation)/([^/]+)$ https://example.software?mode=payment&name=$2 [L]

Header always set Content-Security-Policy "upgrade-insecure-requests;"
Header append X-Frame-Options: "SAMEORIGIN"
Header append X-Content-Type-Options: "nosniff"

已采纳解答

原帖链接 · Stephen Ostermiller · CC BY-SA 4.0

Your rewrite rules use absolute URLs that include the protocol and host name. Internal rewrites only work with relative URLs that have just the path. When mod_rewrite encounters an absolute URL for the target of the rewrite, it uses a redirect.

So instead of

RewriteRule ^home$ https://example.software\?mode\=home&%{QUERY_STRING}  [L]

You should use

RewriteRule ^home$ /\?mode\=home&%{QUERY_STRING}  [L]

There are some other possible problems with that rule. I don't think so much needs to escaped, rather than using a variable to append the query string, you should use the QSA (query string append) flag, and you probably need to rewrite to your index handler. The correct rewrite is probably going to look like:

RewriteRule ^home$ /index.php?mode=home  [QSA,L]

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

资料来源与适用范围

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

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

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