.htaccess ERR_TOO_MANY_REDIRECTS - .htaccess

I got this error ERR_TOO_MANY_REDIRECTS on chrome when I add this code to my .htaccess:
# Redirect old Page URLs to new URLs
# Old URL format: http://example.com/?page=pagename
# New URL format: http://example.com/page/pagename
RewriteCond %{QUERY_STRING} ^page=(.*)$
RewriteRule ^(.*)$ page/%1? [R=301,L]
# Redirect old Search URLs to new URLs
# Old URL format: http://example.com/search.php?search=keyword
# New URL format: http://example.com/search/keyword
RewriteCond %{REQUEST_URI} search.php
RewriteCond %{QUERY_STRING} ^search=(.*)$
RewriteRule ^(.*)$ search/%1? [R=301,L]
# Redirect old Post URLs to new URLs
# Old URL format: http://example.com/post.php?id_post=1
# New URL format: http://example.com/post/1
RewriteCond %{REQUEST_URI} post.php
RewriteCond %{QUERY_STRING} ^id_post=([0-9]*)$
RewriteRule ^(.*)$ post/%1? [R=301,L]
# Support new SEO-friendly URLs
RewriteRule page/(.*) ?page=$1
RewriteRule search/(.*) search.php?search=$1
RewriteRule post/(.*) post.php?id_post=$1
Could you guys tell me wheres the error please, already tried to figure it out but couldn't find what the error is.

Your rules are internally rewriting the target url to itself. You need to use %{THE_REQUEST} to prevent this behavior as these variables do not match internal redirects.
RewriteEngine on
#--Redirect from "/?page=foo" to "/page/foo"--#
RewriteCond %{THE_REQUEST} /\?page=([^\s]+) [NC]
RewriteRule ^ /page/%1? [NC,L,R]
#--Rewrite "/page/foo/" to "/?page=foo"--#
RewriteRule ^page/([^/]+)/?$ /?page=$1 [NC,L,QSA]
#--Redirect from "/search.php?search=foo" to "/search/foo"--#
RewriteCond %{THE_REQUEST} /search\.php\?search=([^\s]+) [NC]
RewriteRule ^ /search/%1? [NC,L,R]
#--Rewrite "/search/foo/" to "/search.php?search=foo"--#
RewriteRule ^search/([^/]+)/?$ /search.php?search=$1 [NC,L,QSA]

Related

301 htaccess redirects with 3 parameters to static urls

Im trying to redirect several urls with 3 parameters to different static urls with .htaccess but nothing working.
1.
http://olddomain.com/index.php?id_category=28&controller=category&id_lang=2
to
https://newdomain.com/page1/
http://olddomain.com/index.php?id_category=30&controller=category&id_lang=2
to
https://newdomain.com/page2/
http://olddomain.com/index.php
to
https://newdomain.com
I tried the below code but http://olddomain.com/index.php not going to https://newdomain.com :
RewriteCond %{HTTP_HOST} ^olddomain.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www.olddomain.com [NC]
RewriteRule ^(.*)$ https://newdomain.com/$1 [L,R=301,NC]
RewriteCond %{QUERY_STRING} ^id_category=28&controller=category&id_lang=2$
RewriteRule ^index.php$ https://newdomain.com/page1/? [R=301,L]
RewriteCond %{QUERY_STRING} ^id_category=30&controller=category&id_lang=2$
RewriteRule ^index.php$ https://newdomain.com/page2/? [R=301,L]
You need to have specific longer matches first and then have rules to remove index.php or domain redirect:
RewriteEngine On
# specific redirects with index.php as optional match
RewriteCond %{QUERY_STRING} ^id_category=28&controller=category&id_lang=2$ [NC]
RewriteRule ^(index\.php)?$ https://newdomain.com/page1/? [R=301,L,NC]
RewriteCond %{QUERY_STRING} ^id_category=30&controller=category&id_lang=2$ [NC]
RewriteRule ^(index\.php)?$ https://newdomain.com/page2/? [R=301,L,NC]
# remove index.php and redirect to newdmain
RewriteCond %{HTTP_HOST} ^(?:www\.)?olddomain\.com$ [NC]
RewriteRule ^(?:index\.php/?)?(.*)$ https://newdomain.com/$1 [L,R=301,NC,NE]
Make sure to clear your browser cache before testing this change.
In case you are taking page's id from id_lang= variable then please try following rules. Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
##Rules to redirect to link: https://newdomain.com/page1/ here.
RewriteCond %{HTTP_HOST} ^(?:www\.)?olddomain\.com$ [NC]
RewriteCond %{THE_REQUEST} \s/index\.php\?id_category=28&controller=category&id_lang=(\d+)\s [NC]
RewriteRule ^ https://newdomain.com/page%1/? [NE,R=301,L]
##Rules to redirect https://newdomain.com/ here.
RewriteCond %{HTTP_HOST} ^(?:www\.)?olddomain\.com$ [NC]
RewriteCond %{THE_REQUEST} \s/index\.php\s [NC]
RewriteRule ^ https://newdomain.com [NE,R=301,L]

OpenCart htaccess redirect to WWW and SSL

I want to force redirect OpenCart store to its WWW version with opencart.
SEO URL Settings
RewriteEngine On
# If your opencart installation does not run on the main web folder make sure you folder it does run in ie. / becomes /shop/
RewriteBase /
RewriteRule ^sitemap.xml$ index.php?route=extension/feed/google_sitemap [L]
RewriteRule ^googlebase.xml$ index.php?route=extension/feed/google_base [L]
RewriteRule ^system/download/(.*) index.php?route=error/not_found [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !.*\.(ico|gif|jpg|jpeg|png|js|css)
RewriteRule ^([^?]*) index.php?_route_=$1 [L,QSA]
RewriteCond %{HTTPS} off
# First rewrite to HTTPS:
# Don't put www. here. If it is already there it will be included, if not
# the subsequent rule will catch it.
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Now, rewrite any request to the wrong domain to use www.
# [NC] is a case-insensitive match
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
This is the htaccess part for the redirect and it works but the smart URL now are
https://www.example.com/index.php?route=the-name-of-the-product
and it should be
https://www.example.com/the-name-of-the-product
Any idea how to fix this problem with the smart urls?
Am I doing the redirect wrong or the problem is other?
Try moving the following lines at the start of .htaccess file
RewriteCond %{HTTPS} off
# First rewrite to HTTPS:
# Don't put www. here. If it is already there it will be included, if not
# the subsequent rule will catch it.
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Now, rewrite any request to the wrong domain to use www.
# [NC] is a case-insensitive match
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Remove subdirectory from URL using htaccess

I have the following URL:
http://example.org/search/search
Using htaccess, I need to remove the first /search from the URL so that the URL is:
http://example.org/search
For the life of me I can't figure out how.
Here's the relevant section of my htaccess file
# Remove www from URL
RewriteCond %{HTTP_HOST} ^www\.example\.org$ [NC]
RewriteRule ^(.*)$ http://example.org/$1 [R=301,L]
# Remove the need for the php file extension
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ http://example.org/$1 [R=302,L]
# Resolve .php file for extensionless php urls
RewriteRule ^([^/.]+)$ $1.php [L]
# Remove index from home URL
RewriteRule ^(.*)index\.php$ http://%{HTTP_HOST}/$1 [R=301,L]
Thanks
Not sure how the rules that you have ever worked. You have some duplicate rules and redirects that interfere with routing. Put all your redirects in the same place, and make sure you use the -f check before you add the php extension:
# Remove www from URL
RewriteCond %{HTTP_HOST} ^www\.example\.org$ [NC]
RewriteRule ^(.*)$ http://example.org/$1 [R=301,L]
# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} \ /+(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ http://example.org/$1 [R=302,L]
# Remove index from home URL
RewriteCond %{THE_REQUEST} \ /+(.*)index\.php
RewriteRule ^(.*)index\.php$ http://%{HTTP_HOST}/$1 [R=301,L]
# Remove the search folder:
RewriteCond %{THE_REQUEST} \ /+search/search
RewriteRule ^search/search(.*)$ /search$1 [L,R=301]
# Add the search folder back
RewriteCond $1 !^/search
RewriteRule ^search(.*)$ /search/search$1 [L]
# Add the extension back on if it exists
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f
RewriteRule ^(.*)$ $1.php [NC,L]

custom 301 redirects from old joomla (mambo) website to new drupal 7 website using .htaccess

The past couple of hours I am trying to create custom redirects from an old mambo website to new drupal 7 website with the .htaccess file that exists in my drupal's root.
What I want to do is...
301 Redirect
http://mysite.com/index.php?option=com_content&task=blogsection&id=11&Itemid=54
to
http://mysite.com/this-is-the-new-page
This is my .htaccess file...
RewriteEngine on
RewriteRule ^index.php?option=com_content&task=blogsection&id=11&Itemid=54$ http://mysite.com/this-is-the-new-page [R=301,L]
RewriteRule "(^|/)\." - [F]
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^ index.php [L]
I am sure that it has something to do with this line...
RewriteRule ^ index.php [L]
But I don't get it!
You see if I use this...
RewriteRule ^option=com_content&task=blogsection&id=11&Itemid=54$ http://mysite.com/this-is-the-new-page [R=301,L]
instead of this...
RewriteRule ^index.php?option=com_content&task=blogsection&id=11&Itemid=54$ http://mysite.com/this-is-the-new-page [R=301,L]
and test it with firefox and LiveHTTP Headers addon it works!
Any suggestions?!
Thanks!
The query string is not part of the URL path pattern. If you want to base a rule on the query string, you must do so in a RewriteCond
RewriteEngine on
RewriteCond %{QUERY_STRING} option=com_content&task=blogsection&id=11&Itemid=54
RewriteRule ^index.php$ /this-is-the-new-page? [R,L]

htaccess redirect from domain.com/index.php?page=about to domain.com/about

I've upgraded an old website and wanted to redirect all the old pages to the new ones. the old website used the index page and a query string to server the relevant content.
here is the new htaccess file. the problem in question is getting the redirect to work. also if you can steamline this file:
Options +FollowSymLinks
Options +Indexes
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^www.domain.co.uk$ [NC]
RewriteRule ^(.*)$ http://domain.co.uk/$1 [R=301,L]
#if its not a directory
RewriteCond %{REQUEST_FILENAME} !-d
#and it has a trailing slash then redirect to URL without slash
RewriteRule ^(.+)/$ /$1 [L,R=301]
#this removes php extention
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
# stops you accessing url with.php
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^.?\ ]+)\.php
RewriteRule ^([^.]+)\.php(/.+)?$ /$1%{PATH_INFO} [R=301]
RewriteRule ^news/([^/]*)/([^/]*)$ /news?id=$1&title=$2 [L]
Redirect 301 index?page=about http://domain.co.uk/about
ErrorDocument 404 http://domain.co.uk/404
The apropiate rule to redirect domain.com/index.php?page=about to domain.com/about is the following
RewriteRule ^index\.php$ %{QUERY_STRING} [C]
RewriteRule page=(.*) /$1? [R=301,L]

Resources