'Redirect url with .htaccess is adding old path
In the same domain, I want to make a 301 redirect from /this/old/path/file.pdf to /this/new/path#abc
I tried this:
Redirect 301 /this/old/path/file.pdf /this/new/path#abc
And it does the redirect but showing the following path:
/this/new/path#abc?/this/old/path/file.pdf
It should be:
/this/new/path#abc
Any hint of what is wrong here?
---Update This is the content of my .htaccess file usign just redirect
RewriteOptions inherit
Options FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
Redirect 301 /this/old/path/file.pdf /this/new/path#abc
</IfModule>
And usign RewriteRule
RewriteOptions inherit
Options FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteCond %{HTTP_HOST} ^mydomain.com$
RewriteRule /this/old/path/file.pdf /this/new/path#abc [NE,R=301]
</IfModule>
This doesn't make any redirect
Don't know if this is important but I am using CPanel.
Solution 1:[1]
The Redirect (mod_alias) directive is applied after the mod_rewrite RewriteRule directives, so you are seeing the query string that is applied by the earlier RewriteRule internal rewrite. You basically have a conflict between mod_alias and mod_rewrite.
You need to use a mod_rewrite RewriteRule directive instead before your existing rules, immediately after the RewriteEngine directive. Not at the end. The order is important.
For example:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com [NC]
RewriteRule ^this/old/path/file\.pdf$ /this/new/path#abc [NE,R=301,L]
Note that the URL-path matched by the RewriteRule pattern does not start with a slash. The first argument to the RewriteRule directive (the pattern) is a regex, so the necessary escaping and anchors need to be applied.
The condition that checks against the HTTP_HOST server variable is necessary if you need this rule to apply only to the stated domain.
The NE flag is required to prevent the # character being URL-encoded in the response (negating its meaning a fragment identifier delimiter).
You will need to clear your browser cache before testing. And test first with 302 (temporary) redirect to avoid potential caching issues.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
