Redirect from one directory to another with mod_rewrite

前端 未结 2 527

The following is my directory structures:

admin\\
controls\\
images\\
media\\
lib\\
models\\
views\\
index.php
.htaccess

The following is m

相关标签:
2条回答
  • 2021-01-12 22:33

    Change the rule as follow:

    RewriteEngine On 
    RewriteRule ^admin/images/(.*) images/$1
    

    And put your .htaccess in your document root, or however in the '/admin' parent folder.

    0 讨论(0)
  • 2021-01-12 22:43

    You need to specify that your image rewrite rule is the last one in the row in order to prevent further rewriting. For that you simply specify [L] at the end of your image rewrite rule.

    RewriteEngine On 
    RewriteRule /admin/images/(.*) /images/$1 [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php
    

    EDIT: Here is an explanation why the problem occurs (taken from the comments section in order to provide clarification).

    You see the original %{REQUEST_FILENAME} will never change no matter how many stages of rewriting are performed. That means that when the second rule is reached that variable will actually still point to the existing image (which is in /admin/images/) rather to the one being rewritten and non-existing (/images/). That's the reason why the second rule will always be applied and why the two conditional lines from the example are almost always the first ones to be used during rewriting.

    0 讨论(0)
提交回复
热议问题