apache mod rewrite 301 get variables showing - .htaccess

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]

Related

Why is my Redirect 301 not working?

I already have a couple of rules set up such as
RewriteCond %{HTTP_HOST} ^www.pipcanadaltd\.com$ [NC]
RewriteRule .?$ http://ca.pipglobal.com%{REQUEST_URI} [R=301,L]
And I also have rewrites for a few PHP pages such as:
RewriteRule ^products/eye-protection-experts/$ prod-expert-eyewear.php [NC,L]
For some reason, when I went to create a simpler 301 redirect, it is not working. Here is what I have:
RewriteRule ^products/construction-channel-experts/$ ^products/construction-safety-solutions/ [R=301]
I'm really confused on why this doesn't work.
You can use this rule for 301 redirect:
RewriteRule ^products/construction-channel-experts/?$ /products/construction-safety-solutions/ [R=301,L,NC]
Note that ^ is used for matching start position in regex and it can only be used in pattern which is on left hand side.
You should keep redirect rules before internal rewrite rules.

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]

How redirect urls with special url?

I want to redirect this type url to my home page
http://www.mywebsite.com/index.php?showuser=
to
http://www.mywebsite.com/
Can I do this with .htaccess? How to do it. I tried this but works nothing.
redirectMatch 301 /index.php?showuser= http://www.mywebsite.com
And
RedirectMatch 301 /index.php\?showuser\= http://www.mywebsite.com
You can't match against the query string in the RedirectMatch regex. You need to use mod_rewrite and the %{QUERY_STRING} variable:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^showuser=
RewriteRule ^index\.php$ /? [L,R=301]
Neither Redirect or RedirectMatch can match the query string. The ? in RedirectMatch actually means "the previous character 0 or 1 times", and would be matched against "index.php". Please note that you are not using the dot correctly too.
You can solve this by using mod_rewrite. Make sure that it is enabled, and that you allow override of FileInfo. Then use the following directives:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^showuser=$
RewriteRule ^index\.php$ / [R,L]
Change [R] to [R=301] if you wish to make the redirect permanent after testing everything works as expected.

.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

Redirect 301 & Mod Rewrite Issues

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]

Resources