301 redirect destination url format - .htaccess

I am trying to do some 301 url redirect at my website, here are the first few lines of the .htaccess file :-
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^?]*) index.php?_route_=$1 [L,QSA]
Redirect 301 /en/home/index.html /index.php?
Redirect 301 /en/about/product.html /shop?
Redirect 301 /en/member/profile.html /index.php?route=account/account
All the above 3 301 rules works fine. But when I remove the "?" at first and second rule, the redirections fail. So in this case, "?" must be present.
However, if I wish to have the format that is without "?", is it possible?
Also, if I just do url rewrite, but not do 301 redirect, will it still maintain the previous seo achievements?

Try using mod_rewrite instead of mod_alias:
RewriteRule ^en/home/index.html$ /index.php? [L,R=301]
RewriteRule ^en/about/product.html$ /shop? [L,R=301]
RewriteRule ^en/member/profile.html$ /index.php?route=account/account [L,R=301]
You're also going to want those before your index.php routing rule.

Related

301 Redirect in htaccess to remove URL Parameter? And redirect to original URL

I have following pattern of URLs in some of my WordPress site
abc.com/xolo-lt900-price-in-india.html? m=1
and
abc.com/xolo-lt900-price-in-india.html? m=0
I want to remove part which is after parameter value and 301 redirect it to main/original URL.
So,
bc.com/xolo-lt900-price-in-india.html?m=1
Shall 301 redirect to
abc.comprice4india.co.in/xolo-lt900-price-in-india.html
And in same way
abc.com/xolo-lt900-price-in-india.html? m=0
Shall 301 redirect to
abc.com/xolo-lt900-price-in-india.html
I have the following code but its not working.
RewriteCond %{QUERY_STRING} m [NC]
RewriteRule ^$ /? [R=301,L]
Thanksfully the old code from #Anubhava is working now..
RewriteCond %{QUERY_STRING} ^m=[01]$ [NC]
RewriteRule ^(.*)$ /$1? [R=301,L,NC]

htaccess 301 redirect to home/index page

I need the solution for home page 301 redirection.
If I enter like below url in the browse bar
http://www.starmed.dk/index.php/component/restaurantguide/tags/tags/2-kebab?sem_midx=-3&sem_jidx=-1
http://www.starmed.dk/index.php/about-us/restaurant/faq.php
http://www.starmed.dk/index.php/about-us/tags/18-pizzeria
http://www.starmed.dk/index.php/about-us/tags/9-online-shop
http://www.starmed.dk/index.php/tilfoj-din-butik/city/47-odder?sem_midx=-1&sem_jidx=-3&format=feed&type=atom
http://www.starmed.dk/index.php/component/restaurantguide/recipes/recipes/20-ca-nuong-trui-bare-fried-fish?sem_midx=3&sem_jidx=1
http://www.starmed.dk/index.php/tilfoj-din-butik/city/95-kalundborg?sem_midx=-6&sem_jidx=0
http://www.starmed.dk/index.php/component/restaurantguide/restaurant/1-frederiks-have?sem_midx=2&sem_jidx=1
http://www.starmed.dk/index.php/tilfoj-din-butik/tags/faq.php
http://www.starmed.dk/?index%5c.php%25253Fid=3-yorkshire-savings-account.83&xzaty=3&article=83
http://www.starmed.dk/?option=com_restaurantguide&view=states&id=450:midtjylland
http://www.starmed.dk/index.php?cPath=56
http://www.starmed.dk/index.php?cPath=25
http://www.starmed.dk/index.php?cPath=47
and etc...
If I enter after index.php some values like mentoned above example
then it will be redirected to http://www.starmed.dk without index.php
How to do this using HTACCESS 301 redirect common rule?
You can use this rule to remove index.php from your URL:
RewriteEngine On
# remove index.php, MAKE SURE THIS IS YOUR FIRST RULE
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteCond %{REQUEST_URI} ^(.*/)index\.php$ [NC]
RewriteRule ^ %1 [L,R=301,NE]

.htaccess 301 redirect from old url to SEO friendly

I have link like this:
www.site.com/page.php?p=1
Need to rewrite it to friendly URLs in htaccess
RewriteRule ^home$ page.php?p=1
It works but now I have two active links with the same content.
Tried to add 301 redirect from old link to new but stuck in loop. Any ideas how to fix that?
Try matching against the actual request so that your rules won't loop:
RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /page\.php\?p=1(&|\ |^)([^\ ]*)
RewriteRule ^page\.php$ /home?%3 [L,R=301]
# then your internal rewrite
RewriteRule ^home$ page.php?p=1
Remove the redirect on the page and handle it in the htaccess.
RewriteRule ^page\.php\?p=1$ /home [L,R=301]
This will redirect to /home and stop the redirect loop you have now.
another quick & dirty way to prevent looping in these situations i've found is to add a querystring and then check for its existence in the redirect.
RewriteCond %{QUERY_STRING} ^p=1
RewriteCond %{QUERY_STRING} !foo=bar
RewriteRule ^page\.php$ /home [NC,R=301,L]
RewriteRule ^home$ page.php?p=1&foo=bar [NC,L]
found on this site: http://answers.oreilly.com/topic/542-how-to-properly-redirect-for-maximum-seo/
Redirect 301 /page.php?p=1 www.yourwebsite.com/home?p=1 RewriteRule
^home?p=1$ /page.php?p=1

Old query String hard coded 301 url redirection using htaccess

I have recently changed CMS of my website and now looking to redirect old urls like:
oldpage.php?key=7cdb93c26
to some new urls but I want all of them in hard coded way and it should be a 301 redirect
I tried the following rules but none of them work for me
redirect 301 /oldpage.php?key=7cdb93c26 http://www.mynewsite.com/my-new-page.html
redirect 301 "/oldpage.php?key=7cdb93c26" http://www.mynewsite.com/my-new-page.html
RewriteRule ^oldpage.php?key=7cdb93c26$ http://www.mynewsite.com/my-new-page.html [L,R=301]
RewriteRule ^oldpage.php?key=7cdb93c26$ /my-new-page.html [L,R=301]
Try this
RewriteCond %{REQUEST_FILENAME} oldpage.php
RewriteCond %{QUERY_STRING} key=7cdb93c26
RewriteRule ^http://www.mynewsite.com/my-new-page.html [L,R=301]
I found the solution myself and here it is:
RewriteCond %{SCRIPT_FILENAME} oldpage.php
RewriteCond %{QUERY_STRING} key=7cdb93c26
RewriteRule ^(.*)$ http://www.mynewsite.com/my-new-page.html? [L,R=301]

Using mod_rewrite and mod_alias (redirect 301) together in .htaccess?

I have a site with a set of old .html and .php pages that have been put into a CMS.
Currently in the .htaccess file there are about 30 mod_alias redirects in the following form:
redirect 301 /oldpage1.html http://www.example.com/newpage1.php
redirect 301 /oldpage2.php http://www.example.com/newpage2.php
redirect 301 /oldpage3.php http://www.example.com/newpage3.php
But we want to use mod_rewrite to have pretty URLs in our CMS, which will take the form http://www.example.com/pagename.php, so also have the following:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?page=$1
At the moment both are being applied together, which results in:
http://www.example.com/newpage1.php?page=oldpage1.html
How can I apply the rewrite rule only when no match has been made by the mod_alias redirect 301 statements, so that the following occurs:
http://www.example.com/oldpage1.html -> redirects to ->
http://www.example.com/newpage1.php -> which is treated as ->
http://www.example.com/index.php?page=/newpage1.php
Any hints would be very much appreciated? Thanks.
I found the answer in a great explanation of mod_rewrite and mod_alias
The problem is that mod_rewrite always occurs before mod_alias, regardless of the order the are placed in .htaccess. This is the reverse of the order required for this situation.
The trick is to use RewriteRule [R=301] instead of redirect 301, and hence use mod_rewrite for everything instead of mixing it with mod_alias.
Full solution is as follows:
RewriteEngine on
RewriteBase /
RewriteRule ^oldpage1.html /newpage1.php [R=301,L]
RewriteRule ^oldpage2.php /newpage2.php [R=301,L]
RewriteRule ^oldpage3.php /newpage3.php [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?page=$1

Resources