Redirect 301 & Mod Rewrite Issues - .htaccess

I have a online store that, for example, is located here: hxxp://domain.com/store/
Within the store directory's .htaccess file I have this:
Options +FollowSymlinks
RewriteEngine on
RewriteBase /store/
RewriteCond %{HTTP_HOST} !^www.domain.com$
RewriteRule ^(.*)$ http://www.domain.com/store/$1 [R=301]
RewriteRule ^/?$ directory.php
RewriteRule ^search/?$ search.php
RewriteRule ^category/([a-zA-Z0-9\!\#\#\$\%\^\&\*\(\)\?\_\-\ ]+)/?$ product.php?CategoryID=$1 [QSA]
RewriteRule ^([a-zA-Z0-9\!\#\#\$\%\^\&\*\(\)\?\_\-\ ]+)/?$ product/detail.php?ProductID=$1 [QSA]
It works great!
My issue (problem) is that I now need to change the /store/ directory to /shop/ which seemed simple enough. However, I need to setup proper 301 redirects so that I don't loose any SE rankings that I may have.
What is the best way to setup 301 redirects in this situation?
The only solution I have found is to setup a redirect 301 for each category, product etc. in the store. Like so, for example.
Redirect 301 /store/category/sample-widgets/ hxxp://www.domain.com/shop/category/sample-widgets/
This works and does what I need it to, but... the URL in the address bar displays like so: hxxp://www.domain.com/shop/category/sample-widgets/?CategoryID=sample-widgets
I can not figure out why or how to remove the query string.
Please help. Thanks.

You can handle the 301 errors by using a PHP script to handle the redirects.
In your .htaccess file you would add this rule:
Redirect 301 /error301.php
Create the file error301.php:
<?php
$redirects = array('/path/to/old/page/' => '/path/to/new/page/',
'/store/category/sample-widgets/' => '/shop/category/sample-widgets/');
if (array_key_exists($_SERVER['REQUEST_URI'], $redirects))
{
$dest = 'http://'.$_SERVER['HTTP_HOST'].$redirects[$_SERVER['REQUEST_URI']];
header("Location: {$dest}", TRUE, 301); // 301 Moved Permanently
}

You could use a simple Redirect directive like:
Redirect 301 /store/ /shop/
Or, if you want to use mod_rewrite as well, you would need to change your current rules as you can not use the base URL /store/ anymore:
RewriteEngine on
RewriteCond %{HTTP_HOST} !=www.example.com
RewriteRule ^ http://www.example.com%{REQUEST_URI} [L,R=301]
RewriteRule ^store/?([^/].*)?$ /shop/$1 [L,R=301]
RewriteRule ^shop/?$ directory.php
RewriteRule ^shop/search/?$ shop/search.php
RewriteRule ^shop/category/([a-zA-Z0-9!##$%^&*()?_\-\ ]+)/?$ product.php?CategoryID=$1 [QSA]
RewriteRule ^shop/([a-zA-Z0-9!##$%^&*()?_\-\ ]+)/?$ product/detail.php?ProductID=$1 [QSA]

Related

How can I redirect old URLs to new URLs using htaccess? [duplicate]

I'm trying to redirect a folder and all its sub files to a URL with a .htaccess file.
But
Redirect 301 /abc/cba/ http://www.aaa.com/
Will make /abc/cba/ddd/index.html redirect to http://www.aaa.com/ddd/index.html
What I want is redirect /abc/cba/ /abc/cba/ddd/index.html to http://www.aaa.com/
Could anyone help? Thanks. If anything not clear, please let me know.
By default, Redirect sort of maps the path node to a new path node, so anything after the first path gets appended to the target URL.
Try:
RedirectMatch 301 ^/abc/cba/ http://www.aaa.com/?
Or if you'd rather use mod_rewrite instead of mod_alias:
RewriteEngine On
RewriteRule ^/?abc/cba/ http://www.aaa.com/? [R=301,L]
here's another example of a mod_rewrite rule that worked for me
I wanted to redirect a sub directory to the root of the same domain.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^sub_directory/(.*)$ /$1 [R=301,NC,L]
</IfModule>
more examples can be found here:http://coolestguidesontheplanet.com/redirecting-a-web-folder-directory-to-another-in-htaccess/
I perfer the following method:
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/somedir [NC]
RewriteRule /(.*) http://somesite.com/lost/$1 [R=301,L]
I had to reroute urls from old site version to new version, so here is what I did to reroute any links from about-us/* to about-us.html
RewriteEngine on
RewriteRule ^about-us/(.*)$ about-us.html [R=301,L]
What it doesn't do is rewrite something like domain.com/about-us/thing.html => domain.com/about-us.html .
It does work for things without extensions domain.com/about-us/something-in-url => domain.com/about-us.html
I added the lines below to redirect .jpg and .png, but it didn't work for .html, I can't find out why.
RewriteRule ^about-us/(.*).jpg about-us.html [R=301,L]
RewriteRule ^about-us/(.*).png about-us.html [R=301,L]

apache mod rewrite 301 get variables showing

I have the following for a page:
RewriteRule ^es/aprende-([^/]+)-online$ learn-language.php?learnLang=$1&lang=es [L]
My new page:
RewriteRule ^es/aprende-ingles-online$ learn-english-online.php?lang=es [L]
Now I want to do a 301 redirect on this page:
RedirectMatch 301 /es/aprende-inglés-online http://www.example.com/es/aprende-ingles-online
But when I go to the page the variables get carried on and appended to the url ?
learnLang=inglés&lang=es
I dont want this get variables to be added on, what do I do?
You need to keep redirect rule before your earlier internal rewrite rule and use mod_rewrite rules only:
RewriteEngine On
RewriteBase /
RewriteRule ^es/aprende-inglés-online/? /es/aprende-ingles-online [L,R=301]
RewriteRule ^es/aprende-ingles-online$ learn-english-online.php?lang=es [L,QSA]
RewriteRule ^es/aprende-([^/]+)-online$ learn-language.php?learnLang=$1&lang=es [L,QSA]

301 redirect subdomain to another domain with htaccess

I want to 301 redirect a subdomain to another domain with htaccess.
I want:
A: www.subdomain.domain1.se
B: subdomain.domain1.se
C: subdomain.domain1.se/anything/anything.anything#anything?anything
to redirect to:
A: www.domain2.se
B: www.domain2.se
C: www..domain2.se/anything/anything.anything#anything?anything
Also I need to know where to put the file (in subdomain directory or root directory). Best would be if I could put the htaccess file in the subdomain ddirectory if possible.
I have tried this:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^c\.domain1\.se$ [NC]
RewriteRule ^(.*)$ http://www.domain2.se/$1 [QSA,R=301,L]
I think you messing RewriteBase / in the code
regular redirect 301 work like this
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)$ http://domain2.com/$1 [L,R=301,NC]
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} subdomain\.domain1\.se$ [NC]
RewriteRule ^ http://www.domain2.se%{REQUEST_URI} [R=301,L,NE]
Reference: Apache mod_rewrite Introduction
However note that URL part after hash is not sent to web server hence cannot be handled by Apache mod_rewrite.

.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

.htaccess redirect folder to a url

I'm trying to redirect a folder and all its sub files to a URL with a .htaccess file.
But
Redirect 301 /abc/cba/ http://www.aaa.com/
Will make /abc/cba/ddd/index.html redirect to http://www.aaa.com/ddd/index.html
What I want is redirect /abc/cba/ /abc/cba/ddd/index.html to http://www.aaa.com/
Could anyone help? Thanks. If anything not clear, please let me know.
By default, Redirect sort of maps the path node to a new path node, so anything after the first path gets appended to the target URL.
Try:
RedirectMatch 301 ^/abc/cba/ http://www.aaa.com/?
Or if you'd rather use mod_rewrite instead of mod_alias:
RewriteEngine On
RewriteRule ^/?abc/cba/ http://www.aaa.com/? [R=301,L]
here's another example of a mod_rewrite rule that worked for me
I wanted to redirect a sub directory to the root of the same domain.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^sub_directory/(.*)$ /$1 [R=301,NC,L]
</IfModule>
more examples can be found here:http://coolestguidesontheplanet.com/redirecting-a-web-folder-directory-to-another-in-htaccess/
I perfer the following method:
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/somedir [NC]
RewriteRule /(.*) http://somesite.com/lost/$1 [R=301,L]
I had to reroute urls from old site version to new version, so here is what I did to reroute any links from about-us/* to about-us.html
RewriteEngine on
RewriteRule ^about-us/(.*)$ about-us.html [R=301,L]
What it doesn't do is rewrite something like domain.com/about-us/thing.html => domain.com/about-us.html .
It does work for things without extensions domain.com/about-us/something-in-url => domain.com/about-us.html
I added the lines below to redirect .jpg and .png, but it didn't work for .html, I can't find out why.
RewriteRule ^about-us/(.*).jpg about-us.html [R=301,L]
RewriteRule ^about-us/(.*).png about-us.html [R=301,L]

Resources