Rewrite rule not working - php

I'm breaking my head in redirecting a few page from my old asp website to my new php website.
For example, I want to redirect permanently the following 2 pages from my asp website to my php website
1) http://www.mywebsite.com/shopdisplayproducts.asp?id=11&cat=food&sortorder=mostrecent&page=1&pagesize=12
TO
http://www.mywebsite.com/index.php?route=product/category&path=10_11
where 11 should be the link between old and new website to display the correct page.
2) shopexd.asp?id=3903&prod=food&vrnt=green&mrk=studio&cat=11
TO
index.php?route=product/product&path=10_11&product_id=3903
where the number 11 is the link between old and new url for the category and the number 3903 is the link for the product number
I have the following rewrite rule in my htaccess page for the first redirect, but it seems not be triggered. What is wrong? Please help.
RewriteCond %{REQUEST_URI} ^/shopdisplayproducts\.asp\?id=([0-9]+).*$ [NC]
RewriteRule ^shopdisplayproducts\.asp\?id=([0-9]+).*$ index.php?route=product/category&path=10_$1 [R=301,L]
I also tried:
RewriteCond %{REQUEST_URI} ^shopdisplayproducts\.asp\?id=([0-9]+).*$ [NC]
RewriteRule ^shopdisplayproducts\.asp\?id=([0-9]+).*$ index.php?route=product/category&path=10_$1 [R=301,L]
Thanks for any help
Sabko

You need to use QUERY_STRING variable in rule condition:
RewriteEngine On
# /shopexd.asp?id=3903&prod=food&vrnt=green&mrk=studio&cat=11 to
# /index.php?route=product/product&path=10_11&product_id=3903
RewriteCond %{QUERY_STRING} ^id=(\d+)&.*&cat=(\d+) [NC]
RewriteRule ^shopexd\.asp$ /index.php?route=product/category&path=10_%2&product_id=%1 [R=301,NC,L]
RewriteCond %{QUERY_STRING} ^id=(\d+) [NC]
RewriteRule ^shopdisplayproducts\.asp$ /index.php?route=product/category&path=10_%1 [R=301,NC,L]

Related

.htaccess; Too many redirects if two different redirects in .htaccess

I've two types of URLs:
http://www.example.com/?content=contact_form.php
and
http://www.example.com/?content=product.php&id=20
I changed my whole URL system like this:
http://www.example.com/file/contact_form
and
http://www.example.com/product/I-m-the-title/20
Of course I made 301 redirect with .htaccess to tell Google and co. the new URL.
I made it like this:
# Rewrite URLs
RewriteEngine On
RewriteRule ^(ignore)($|/) - [L]
RewriteRule ^file/([^/]*)$ /?content=$1.php [L]
RewriteRule ^([^/]*)/([^/]*)/([^/]*)$ /?content=$1.php&title=$2&id=$3 [L]
# Redirect old URL to new URL
RewriteCond %{QUERY_STRING} ^content=contact\_form\.php$
RewriteCond %{REQUEST_URI} ^\/$
RewriteRule .* http://www.example.com/file/contact_form? [R=301,L]
RewriteCond %{QUERY_STRING} ^content=product\.php&class=I-m-the-title$
RewriteCond %{REQUEST_URI} ^\/$
RewriteRule .* http://www.example.com/I-m-the-title/Test/20? [R=301,L]
My problem:
It's perfectly working for: http://www.example.com/?content=product.php&id=20
But for http://www.example.com/?content=contact_form.php I'm getting the message that it couldn't get opened because of too much redirect.
Does anybody know what I'm doing wrong? I hope anybody can help me soon because I have to fix it before Google misinterprets it.
Your rule cause an infinite loop because it is rewriting your uri to the same location again and again overriding your internal and external redirects.. To fix the Rewrite loop, add the following at the top of your htaccess
RewriteEngine on
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]

Redirect domain.com/shop to www.domain.com/shop

Note: Before you mark it duplicate, please read the complete question.
I already applied bellow suggestion which didn't work for me.
1 - mod rewrite from .domain to www.domain htaccess
2 - .htaccess redirect from http://www.domain/ to https://domain.com
3 - .htaccess redirect any domain/directory to www.domain/directory
We've got a shopping site which we're hosting on a host (CloudLogin).
The main site is built on Joomla CMS and Magento as shopping cart.
The main site URL is www.domain.com and shopping cart URL is domain.com/shop (Without www).
What we need is to show both URLs with (www).
So we need the Magento folder ie domain.com/shop also to be on www URL.
I found some solutions on stack overflow but they didn't work for me as it always show a message browser (Too many redirection or /page isn't redirecting properly)
Note: The .htaccess file is located in (shop) folder.
Bellow is what I tried so far:
1 -
# anything.com to www.anything.com
RewriteCond %{HTTP_HOST} ^[^.]+(\.[^.]+)?$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=301,NE]
The above code removes the (shop) directory from URL which turns into error.
ex: domain.com/shop/cat1/prod1 turns into www.domain.com/cat1/prod1
2 -
RewriteCond %{REQUEST_URI} !^/shop/?.*$
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]
Above code not even apply www on it.
3 -
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Above code redirects from domain.com/shop/cat1/prod1 to www.domain.com/shop with error message (redirected you too many times.)
Which seems infinite loop on page.
4 -
RewriteCond %{HTTP_HOST} ^domain.com$ [NC]
RewriteRule ^(.*) http://www.domain.com/shop/$1 [L,R=301]
This does the same thing as 3.
5 -
#RewriteCond ${HTTP_HOST} doamin.com [OR]
#RewriteCond ${HTTP_HOST} www.doamin.com
#RewriteRule ^(.*)$ http://www.doamin.com/shop/$1 [QSA,R=301,L]
Does anyone know how I can always force the correct use of .htaccess for particular URLs?
I've had a look around SO but couldn't find a suitable answer to this.
Try :
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/shop
RewriteCond www.%{HTTP_HOST} ^(?:www\.)?(www\..+)$ [NC]
RewriteRule ^ http://%1/shop%{REQUEST_URI} [NE,L,R]
Did you try the following?
# anything.com to www.anything.com
RewriteCond %{HTTP_HOST} ^[^.]+(\.[^.]+)?$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/shop/$1 [L,R=301,NE]
Since requests originate from the /shop directory they will not contain that directory in the URL requested.

Redirect everything after a Querystring to another page using mod_rewrite

I am working on a site that is featuring two multiple page redirects based on query strings. The problem is that these pages are now giving us a 404 error
URL 1 is something like
website.com?queryhome=productname-546-axdd
URL 2 is something like
website.com?querynonhome=productname-546-asx
I am trying to get everything in URL 1 to go to homepage and URL 2 to goto website.com/reviews-page
I tried the following for URL 1
RewriteCond %{QUERY_STRING} ^queryhome=([0-9]*)$
RewriteRule (.*) http://www.website.com [R=301,L]
and the following for URL 2
RewriteCond %{QUERY_STRING} ^querynohome=([0-9]*)$
RewriteRule (.*) http://www.website.com/reviews-page [R=301,L]
But neither seems to work.
Any sort of help in this matter would be appreciated. As is evident, this is the first time I am trying to tackle a URL which has a query string.
Thank you
Your regex is the problem here:
RewriteCond %{QUERY_STRING} ^queryhome=([0-9]*)$
RewriteRule (.*) http://www.website.com [R=301,L]
Which will match query string queryhome=1234 due to [0-9]* only but NOT queryhome=productname-546-axdd
You need to use following rules:
RewriteCond %{QUERY_STRING} ^queryhome=([^&]*) [NC]
RewriteRule ^ http://www.website.com/? [R=301,L]
RewriteCond %{QUERY_STRING} ^querynohome=([^&]*) [NC]
RewriteRule ^ http://www.website.com/reviews-page? [R=301,L]
Please read: Apache mod_rewrite Introduction

Subdomain rewrite help for joomla SEF urls

been stuck at this for the past couple of hours here. Let me just illustrate my problem simply by the following lines.
Here's
1) Joomla absolute URL for the page i'm trying to convert: http://classifiedads4free.com/index.php?option=com_adsmanager&view=front
then:
Created menu item and activated SEF urls: So now alias becomes http://classifiedads4free.com/ads
after which:
I added a query string at the back of the URL so it becomes: http://classifiedads4free.com/ads?country=Singapore.
Was just wondering if there's anyway i can make the url at the top http://classifiedads4free.com/ads?country=Singapore. -->http://singapore.classifiedads4free.com
Tried going back to using the absolute URL on my .htaccess file, Following is the code:
1st tried:
RewriteCond %{HTTP_HOST} ^(.+)\.classifiedads4free\.com$
RewriteCond %{HTTP_HOST} !^www\.classifiedads4free\.com$
RewriteCond %{REQUEST_URI} !^.*\.(jpe?g|png|gif|bmp)$ [NC]
RewriteRule (.*) ads?country=%1 [L]
2nd tried:
RewriteCond %{HTTP_HOST} ^(.+)\.classifiedads4free\.com$
RewriteCond %{HTTP_HOST} !^www\.classifiedads4free\.com$
RewriteCond %{REQUEST_URI} !^.*\.(jpe?g|png|gif|bmp)$ [NC]
RewriteRule (.*) index.php?option=com_adsmanager&view=front&country=%1 [L]
However, I can't seem to get the redirect working. It keeps redirecting me back to my home page which isn't what i requested.
Would appreciate if someone could provide some assistance here. Been trying all day long.
Try this:
RewriteRule ^(.*)$ http://www.classifiedads4free.com/ads?country=%1 [L,NC]

I want to redirect old url to new rewrite url using htaccess or php

I have a problem since i using rewrite url..
MY OLD URL:
Website.com/index.php?act=appdetail&appid=oWV
New Rewrite URL
http://website.com/angry_birds_rio-appdetail-oWVi.html
But all my old url are indexed in google and if any one come to my website its display the old URL and google also INDEXED the NEW URL. its make duplicate page on website problem.
Let me know the solution
My rewrite URL htaccess
RewriteEngine On
RewriteRule ^([^-])-([^-])-([^-])-([^-])-([^-]*).html$ index.php?appanme=$1&act=$2&appid=$3&page=$4&cat=$5 [L]
RewriteRule ^([^-])-([^-])-([^-])-([^-])-([^-])-([^-]).html$ index.php?appanme=$1&act=$2&appid=$3&page=$4&cat=$5&sString=$5 [L]
RewriteRule ^([^-])-([^-])-([^-]*).html$ index.php?appanme=$1&act=$2&appid=$3[L]
Here is your .htaccess file:
RewriteEngine on
RewriteRule ^/index.php?act=appdetail&appid=oWV$ http://website.com/angry_birds_rio-appdetail-oWVi.html [R=301,L]
You'll need to inform to web crawlers about the redirection, you cando it with a 301 code.
Appears the rule are .htaccess based; you need an additional set of rules to permanently redirect (301) BROWSER/CRAWLER requests for the index.php pages, if a set of CGI arguments are present, to the appropriate alias, this will tidy up Google in a few weeks. Then your rules above e.g.
RewriteEngine On
RewriteBase /
#Permanently redirect BROWSER requests for the index.php?xxx to the appropriate page alias:
RewriteCond %{THE_REQUEST} /index.php [NC]
RewriteCond %{QUERY_STRING} ^appanme=([^&]+)&act=([^&]+)&appid=([^&]+)&page=([^&]+)&cat=([^&]+)&sString=([^&]+) [NC]
RewriteRule ^.* http://%{HTTP_HOST}/%1-%2-%3-%4-%5-%6.html [R=301,L]
RewriteCond %{THE_REQUEST} /index.php [NC]
RewriteCond %{QUERY_STRING} ^appanme=([^&]+)&act=([^&]+)&appid=([^&]+)&page=([^&]+)&cat=([^&]+) [NC]
RewriteRule ^.* http://%{HTTP_HOST}/%1-%2-%3-%4-%5.html [R=301,L]
RewriteCond %{THE_REQUEST} /index.php [NC]
RewriteCond %{QUERY_STRING} ^appanme=([^&]+)&act=([^&]+)&appid=([^&]+) [NC]
RewriteRule ^.* http://%{HTTP_HOST}/%1-%2-%3.html [R=301,L]
# Followed by: YOUR RULES FROM ABOVE
Note:
1) There appears to be a typo in YOUR second rule: sString=$6 NOT sString=$5
2) The Apache mod_rewrite documentation is worth a read if your unclear as to what the above rules do, or if you want something a little more abstract consider the following post.

Categories