26682

Remove double 301 redirect from my htaccess?

Question:

I just realized that my htaccess produces double 301 redirect in some cases. For example, if you try to access <a href="http://example.com/old_url" rel="nofollow">http://example.com/old_url</a> it will:

<ul><li>First 301 to <a href="http://www.example.com/old_url" rel="nofollow">http://www.example.com/old_url</a> (added www)</li> <li>Then 301 to <a href="http://www.example.com/new_url" rel="nofollow">http://www.example.com/new_url</a> (changed to new url)</li> </ul>

Here's how my htaccess is set up:

RewriteEngine On # Add www RewriteCond %{HTTP_HOST} ^example.com [nocase] RewriteRule ^(.*) http://www.example.com/$1 [last,redirect=301] # Do some url rewriting RewriteRule ^new_url_1$ new_url_1.php [NC,L] RewriteRule ^new_url_2$ new_url_2.php [NC,L] # Do the 301 redirections Redirect 301 /old_url_1 http://www.example.com/new_url_1 Redirect 301 /old_url_2 http://www.example.com/new_url_2

How can I fix that to have only one 301 for better SEO?

Answer1:

Don't mix Redirect directives with mod_rewrite rules and keep specific redirect rules before www rule:

RewriteEngine On ## Unless directory, remove trailing slash RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.+)/$ /$1 [NE,R=302,L] # Do specific 301 redirections RewriteRule ^old_url_1$ http://www.example.com/new_url_1 [L,NC,R=301] RewriteRule ^old_url_2$ http://www.example.com/new_url_2 [L,NC,R=301] # Add www RewriteCond %{HTTP_HOST} !^www\. [NC] RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,NE,R=301] # Do some url rewriting RewriteRule ^new_url_1$ new_url_1.php [NC,L] RewriteRule ^new_url_2$ new_url_2.php [NC,L]

Make sure to clear your browser cache before testing.

Recommend

  • Python if/elif syntax error… WHY [duplicate]
  • SQLite sort special characters alphabetically
  • Mod Rewrite and passing a URL as parameter
  • RewriteCond match for certain Query param/value pair
  • SilverStripe. How to redirect from www.domainName to domainName?
  • Using .htaccess to rewrite dynamic subdomains
  • htaccess mod_rewrite URL within subfolder
  • Redirect only with specific domain
  • .htaccess mod_rewrite Unknown number of Variables of a GET form
  • Upload large files 100mb+ to Sharepoint 2010 via c# Web Service
  • Python : Replacing Values in netcdf file using netCDF4
  • Redirecting codeigniter project to https with htaccess: never complete
  • Android SQLite queries marked as Error by Android Studio
  • .htaccess rewriteRule conflict
  • DSP - get the amplitude of all the frequencies
  • IDA Python - Why My code return incorrect ESP Value?
  • How do we specify the client area size of a form in design mode?
  • Too many Redirects after switching WordPress to https
  • htaccess will not rewrite subdomains
  • .htaccess rewrite: subdomain as GET parameter and filepath afterdomain intact
  • Smart URL with optional parameters?
  • Removing the .php file extension from the URL Request
  • Mod rewrite redirection to another domain if file not exist
  • Nginx rewrite equivalent to Apache RewriteRule that converts URL params into QueryString key/value p
  • htaccess add www if not subdomain, if subdomain remove www
  • htaccess rewriting URLs with multiple forward slashes
  • How to make Safari send if-modified-since header?
  • Adding custom controls to a full screen movie
  • Circular dependency while pushing http interceptor
  • A cron job substitute?
  • json Serialization in asp
  • AngularJs get employee from factory
  • How to set the response of a form post action to a iframe source?
  • Angular 2 constructor injection vs direct access
  • Java static initializers and reflection
  • Change div Background jquery
  • Android Google Maps API OnLocationChanged only called once
  • Qt: Run a script BEFORE make
  • reshape alternating columns in less time and using less memory
  • UserPrincipal.Current returns apppool on IIS