David
David

Reputation: 3348

.htaccess: 301 redirect not working in .htaccess in rewritten root path

I have a domain called example.com which points to /public_html/ on my server.

In /public_html/.htaccess I have the following code to change the root path for my domain example.com to /public_html/example_path/:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.example\.com
RewriteRule !example_path/ /example_path%{REQUEST_URI} [L]

This is working very well.

Now, I want to redirect example.com/test.png to example.com/images/test.png. To do this, I'm using the following code in /public_html/example_path/.htaccess:

RewriteEngine On
Redirect 301 /test.png /images/test.png

Unfortunately, this isn't working, there's no redirection. The redirect is only working if I put the code in /public_html/.htaccess but not in /public_html/example_path/.htaccess.

Why that?

Upvotes: 1

Views: 113

Answers (1)

anubhava
anubhava

Reputation: 785531

Inside your /public_html/.htaccess have redirect rule before example_path rule:

RewriteEngine On

RewriteRule ^test\.png$ /images/test.png [L,NC,R=301]

RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule !example_path/ /example_path%{REQUEST_URI} [L,NC]

Note that you cannot have image redirection rule inside /example_path/.htaccess because your image URL doesn't have /example_path/.

Upvotes: 1

Related Questions