Redirect url with .htaccess is adding old path

Carlos Sivira

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.

MrWhite

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

redirect old url with .htaccess

htaccess redirect adding text to url

htaccess redirect url by adding parameters

htaccess redirect old URL to new URL

htaccess redirect from old url to new url

Redirect old url to new url with .htaccess redirection

.htaccess redirect old URL to new URL

htaccess: redirect old asp url strings

.htaccess URL redirect old link format to new

htaccess redirect condition to redirect old URL to new URL

Htaccess Redirect specified url with path to other url

How to redirect 301 old url path to new url path

how to redirect old url with parameters to new seo url using htaccess

301 redirect for old URL's with regular expression through .htaccess

.htaccess redirect old url syntax to new Rewrite Rule syntax

WordPress - redirect with .htaccess an old .aspx link to external URL

.htaccess redirect all pages and url from old domain to new domain

htaccess redirect dynamic old file to new File url

htaccess - 301 redirect url to another url adding more to domain

Adding parameters to a .htaccess redirect

Redirect with htaccess from path based to query based URL but exclude robots

.htaccess redirect and rewrite url if REQUEST_URI starts with specific path

Conditional redirect in .htaccess depending on the number of path segments in the URL

.htaccess redirect from subdirectory to parent directory file but maintain url path

htaccess redirect without the path

Redirect to internal path .htaccess

htaccess redirect from path/path to path?path

Dynamic url's redirect from old to new url's using .htaccess

How to use .htaccess file to redirect old URL (with article id) to new URL (without article id)?