问题原文
原帖链接 · Mantykora 7 · CC BY-SA 4.0
I am wondering, why folder renaming doesn't work. RewriteRule ^pl/(.*) public/$1
Let's say I have a URL like:
example.com/public/index.html
Now I want to rewrite the URL to
example.com/pl/index.html
pl is not a folder rather just a name!
The rest works fine. Why just that one rule doesn't work?
My .htaccess
RewriteEngine on
RewriteRule ^dekoracyjne.html$ decorative.php [NC,L]
RewriteRule ^pl/(.*) public/$1
RewriteRule ^dekoracyjne/(.*) decorative/$1
RewriteRule ^techniczne.html$ architectural.php [NC,L]
RewriteRule ^techniczne/(.*) architectural/$1
RewriteRule ^show.html/([-\w]+)$ show.php?id=${products:$1} [L]
RewriteRule ^p.html$ product_configurator.php [NC,L]
RewriteRule ^show_c\.html/([-*\w]+)$ show_c.php?cat=${catcolors:$1} [L]
RewriteRule ^(product_configurator)\.html/(\d+)&([-\w]+)$ $1.php?prometheus_id=$2&id=${products:$3} [L]
RewriteRule ^(ss_c)\.html/(\d+)&([-\w]+)$ $1.php?prometheus_id=$2&id=${products:$3} [L]
# Rewrite other ".html" requests to ".php"
RewriteRule (.+)\.html$ $1.php [L]
已采纳解答
原帖链接 · MrWhite · CC BY-SA 4.0
This "doesn't work" in your example because of path-info (additional pathname information) on the originally requested URL. Apache mod_rewrite appends this path-info after the rewrite which conflicts with the later rewrite to replace the .html extension with .php.
When you request /pl/index.html, where /pl does not exist as a physical directory then the URL-path that follows, ie. /index.html is path-info. This path-info is re-appended after the first rewrite.
For example, given a request for /pl/index.html:
RewriteRule ^pl/(.*) public/$1- this rewrites the request topublic/index.html.- mod_rewrite then appends the path-info from the original request to become
public/index.html/index.html. RewriteRule (.+)\.html$ $1.php [L]- This directive is now matching againstpublic/index.html/index.html, which results in the request being rewritten topublic/index.html/index.php- which is not the intention and most probably results in a 404 (unlesspublic/index.htmlexists andAcceptPathInfo Onis set).
There are three ways you can resolve this, either:
Add the
Lflag to the initialRewriteRule:RewriteRule ^pl/(.*) public/$1 [L]This might seem counterintuitive as you still want the later
RewriteRuleto change the file extension - but it still does. TheLflag does not stop all processing, it only stops the current round of mod_rewrite processing and (in a directory context) causes the rewrite engine to start over. Part of the process of "starting over" is that the newly rewritten request is remapped to the file system (and path-info is recalculated), so the laterRewriteRuleto replace the.htmlextension is applied to the rewritten URL as expected (since no other rewrites occur in the meantime).The rewrite process loops in this way until the URL passes through unchanged. (Or, the
ENDflag (Apache 2.4) is encountered.)
OR
Add the
DPI(Discard Path-Info) flag to the initialRewriteRuleinstead:RewriteRule ^pl/(.*) public/$1 [DPI]This discards the path-info from the current rewrite (and all future rewrites) thus preventing the path-info (ie.
/index.htmlin this case) from being reappended.
OR
Change the pattern in the last
RewriteRule(that replaces the.htmlfile extension) so that it matches the first.htmlfile extension, and not the last file extension. So as to avoid matching against the appended path-info (if present). For example:RewriteRule ^(.+?)\.html $1.php [L]Note the trailing
$(end-of-string) anchor on theRewriteRulepattern is removed and the captured subpattern is made non-greedy (.+?) so as to match the part of the URL-path before the first.htmland not the last.html. Any additional path-info (if any) - is also discarded by the rewrite.
Which method to choose?
Instinctively, I would simply add the L flag. However, adding the DPI flag instead may in fact be more optimal (it prevents the unnecessary additional loop by the rewrite engine). However, if you add the DPI flag I would consider reordering (and grouping) the rules to make it more obvious/readable that it is the intention that the URL could be further rewritten for these requests. (It looks like you would need to apply this same process to two other rules as well?)
The third option to change the pattern in the last RewriteRule feels a bit hacky and I imagine could potentially encounter edge cases that break depending on your URL structure. Although this could still work OK. (Again, reordering of the rules to make the intention more obvious is recommended.)
For example:
RewriteEngine on
RewriteRule ^dekoracyjne.html$ decorative.php [NC,L]
RewriteRule ^techniczne.html$ architectural.php [NC,L]
RewriteRule ^show.html/([-\w]+)$ show.php?id=${products:$1} [L]
RewriteRule ^p.html$ product_configurator.php [NC,L]
RewriteRule ^show_c\.html/([-*\w]+)$ show_c.php?cat=${catcolors:$1} [L]
RewriteRule ^(product_configurator)\.html/(\d+)&([-\w]+)$ $1.php?prometheus_id=$2&id=${products:$3} [L]
RewriteRule ^(ss_c)\.html/(\d+)&([-\w]+)$ $1.php?prometheus_id=$2&id=${products:$3} [L]
RewriteRule ^pl/(.*) public/$1 [DPI]
RewriteRule ^dekoracyjne/(.*) decorative/$1 [DPI]
RewriteRule ^techniczne/(.*) architectural/$1 [DPI]
# Rewrite other ".html" requests to ".php"
RewriteRule (.+)\.html$ $1.php [L]
Aside: You've included the NC flag on two of the rewrites. This naturally makes the initial match case-insensitive - which shouldn't be necessary. If this is required then you potentially have a duplicate content issue, since foo.html is strictly different to FOO.html.
摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。
