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

Rewrite three URL directories to corresponding PHP files and pass subdirectory as a parameter

· 52CE 我爱测

I want to convert the following https://example.com/download-file/xyz https://example.com/download-code/abc https://example.com/developer/mno to https://example.com/download-file?name=xyz https://example.com/download-code?name=ab…

问题原文

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

I want to convert the following

https://example.com/download-file/xyz
https://example.com/download-code/abc
https://example.com/developer/mno

to

https://example.com/download-file?name=xyz
https://example.com/download-code?name=abc
https://example.com/developer?name=mno

and I would like the param versions to work as well, as they are published.

I tried this, and it works. But it shows the new path in the browser. How can I fix this? And it would be nice to perhaps combine the first two or all 3 into one.

RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteRule ^(download-file)/([^/]+)$ https://example.com/download-file?name=$2 [L]
RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteRule ^(download-code)/([^/]+)$ https://example.com/download-code?name=$2 [L]
RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteRule ^(developer)/([^/]+)$ https://example.com/developer?name=$2 [L]

The only rules before this are

RewriteEngine On

#com
RewriteCond %{HTTPS} =on
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]

RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteRule ^(.*)$ https://example.com%{REQUEST_URI} [R=301,L,QSA]

#nz
RewriteCond %{HTTP_HOST} ^(www\.)?example\.nz$ [NC]
RewriteRule ^(.*)$ https://example.com/$1 [R=302,L]

# Rewrite to ".php" then html
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(?!.+\.\w{2,4})(.+)$ $1.php [L]
RewriteCond %{DOCUMENT_ROOT}/$1.html -f
RewriteRule ^(?!.+\.\w{2,4})(.+)$ $1.html [L]

and the only thing after the rules of concern is gzip and security headers.

Finally, this is the primary domain, so there are no other .htaccess rules in play.

已采纳解答

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

RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteRule ^(download-file)/([^/]+)$ https://example.com/download-file?name=$2 [L]

By using an absolute URL (with scheme + hostname) in the substitution string it will implicitly trigger a 302 (temporary) redirect. To "internally redirect" (or "rewrite") the URL you must specify a relative (or root-relative) URL-path (or file-path).

As mentioned in comments, https://example.com/download-file does not appear to be a valid end-point. You've stated these are .php files, which you are currently relying on other rules to perform a second rewrite to append the file extension. You should instead perform this in a single rule/rewrite.

There does not appear to be a need to check the requested hostname since these URLs are unique to a particular hostname. However, since you are hosting multiple hostnames on the same account (in the same file space?) it may be beneficial to check the requested host to avoid accidental duplicate content (due to some other error that might occur in linking). Although if that is not an issue (eg. other hostnames are canonicalised or point to other file-paths) then you can remove the condition that checks the requested hostname.

In the rule above there is no need for the capturing subgroup around the first path segment, eg. (download-file) since this is not being used later and is forcing you to use $2, when $1 is sufficient. However, if you want to combine these 3 rules then you do need the capturing subgroup.

Try the following instead, combining all 3 rules:

RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule ^(download-file|download-code|developer)/([^/]+)$ $1.php?name=$2 [END]

And this rule should go before the rules that append the .php (and .html) file extensions.

Providing you are on Apache 2.4+, it is more efficient to use the END flag here instead of L, in order to prevent any further looping by the rewrite engine.

The $1 backreference contains the file basename (less the .php extension) and $2 is the URL parameter value, as before.

Disable MultiViews

Note that, since you are rewriting to the same .php file as in the requested URL (just less the .php extension) you need to ensure that MultiViews is disabled for this rule(s) to work. It must already be disabled, otherwise your "redirect" would not have "worked". However, for your code to be portable you should explicitly disable this at the top of your .htaccess file:

# Ensure that MultiViews is disabled
Options -MultiViews

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

资料来源与适用范围

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

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

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