问题原文
原帖链接 · nellyserver · CC BY-SA 4.0
I have a URL structure like this: example.com/this/is/nice/index.php
and I want achieve this: example.com/this/index.php
How can I add a rule to hide the /is/ and /nice/ subfolders ?
已采纳解答
原帖链接 · MrWhite · CC BY-SA 4.0
This assumes you have already changed all the internal links on your site to be of the form /this/index.php (or /this/<something>?), ie. #3 in @Stephen's comment above.
For your specific example, you would add something like the following, using mod_rewrite, to the top of the root .htaccess file (requires Apache 2.4):
RewriteEngine On
# A) Redirect "/this/is/nice/index.php" to "/this/index.php"
RewriteRule ^(this)/is/nice/(index\.php)$ /$1/$2 [R=301,L]
# B) Rewrite "/this/index.php" to "/this/is/nice/index.php"
RewriteRule ^(this)/(index\.php)$ $1/is/nice/$2 [END]
The $1 and $2 backreferences contain the value of the corresponding captured subgroup in the preceding RewriteRule pattern (first argument). In this example, that just saves having to repeat "this" and "index.php".
For a more generic solution that rewrites /this/<anything> to /this/is/nice/<anything> (and redirects vice-versa) then use something like the following instead:
# A) Redirect "/this/is/nice/<anything>" to "/this/<anything>"
RewriteRule ^(this)/is/nice/(.*) /$1/$2 [R=301,L]
# B) Rewrite "/this/<anything>" to "/this/is/nice/<anything>"
RewriteRule ^(this)/(.*) $1/is/nice/$2 [END]
Note that this rewrites everything, including any static assets - which may or may not be a problem depending on your scenario (not stated in the question). eg. /this/images/myimage.jpg would be rewritten to /this/is/nice/images/myimage.jpg, regardless of whether /this/images/myimage.jpg exists or not.
摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。
