301 Redirects with dynamic URLs - .htaccess

I am trying to rewrite my .htaccess file to redirect old dynamic URLS to new ones.
I have tried these so far but they are not working as expected. The second rule seems to redirect to the first one.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^mydomain.com$
RewriteRule ^(.*)$ "http\:\/\/www\.mydomain\.com\/$1" [R=301,L]
Redirect 301 /product.php?l=old-product-name1 http://www.mydomain.com/product.php?l=new-product-name1
Redirect 301 /product.php?l=old-product-name2 http://www.mydomain.com/product.php?l=new-product-name2
Can anyone help me redirect these properly?

RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^mydomain.com$
RewriteRule ^(.*)$ "http\:\/\/www\.mydomain\.com\/$1" [R=301,L]
RewriteCond %{QUERY_STRING} =l=old-product-name1
RewriteRule ^product.php$ http://www.mydomain.com/product.php?l=new-product-name1 [R=301]

Related

301 Redirect to new domain with some specific URLs

I saw similar topics but couldn't find a practical answer to my problem.
I'm moving my old website to a new one, and some URLs are changing.
I would like to make a generic 301 redirection to the new domain (because most paths are the same), while individually redirecting some URLs.
Here is what I have on my old website .htaccess :
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^old\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.old\.com$
RewriteRule (.*)$ https://new.com/$1 [R=301,L]
Redirect 301 "/custom/url/" "https://new.com/my-custom-url"
</IfModule>
But the 301 redirects to : https://new.com/custom/url instead of https://new.com/my-custom-url
Some of my URLs also have URL parameters I would like to redirect, such as :
Redirect 301 "/brand.php?name=Example" "https://new.com/Example"
Redirect 301 "/brand.php?name=Example2" "https://new.com/another/url"
which do not seem to work as well.
Thank you very much for your help.
But the 301 redirects to : https://new.com/custom/url instead of https://new.com/my-custom-url
It is because your specific redirect rule appears after generic one. Moreover you are mixing mod_rewrite rules with mod_alias rules and these are invoked at different times.
Have it like this:
RewriteEngine On
# redirect /brand.php?name=Example2 to new.com/another/Example2
RewriteCond %{HTTP_HOST} ^(www\.)?old\.com$ [NC]
RewriteCond %{QUERY_STRING} ^name=(Example2) [NC]
RewriteRule ^brand\.php$ https://new.com/another/%1? [R=301,L,NE]
# redirect /brand.php?name=Example3 to new.com/category/Example3
RewriteCond %{HTTP_HOST} ^(www\.)?old\.com$ [NC]
RewriteCond %{QUERY_STRING} ^name=(Example3) [NC]
RewriteRule ^brand\.php$ https://new.com/category/%1? [R=301,L,NE]
# generic redirect /brand.php?name=Example to new.com/Example2
RewriteCond %{HTTP_HOST} ^(www\.)?old\.com$ [NC]
RewriteCond %{QUERY_STRING} ^name=([^&]+) [NC]
RewriteRule ^brand\.php$ https://new.com/%1? [R=301,L,NE]
# redirect custom URL
RewriteRule ^custom/url/ https://new.com/my-custom-url [R=301,L,NE,NC]
# redirect everything else
RewriteCond %{HTTP_HOST} ^(www\.)?old\.com$ [NC]
RewriteRule ^ https://new.com%{REQUEST_URI} [R=301,L]

htaccess redirect not working for inner pages / folders

Ok, so I've got a stack of htaccess redirects from one domain to another, which for the most part works fine. However, this particular link isn't working:
http://giccampers.com.au/general-trailers/tipper-trailer/
It should take people to this website:
http://blackseriescampertrailers.com.au/
If you go to the main domain of http://giccampers.com.au/ it redirects fine. But I can't seem to get any of the other pages to redirect from that domain.
Here's what I've got (I didn't set these up, btw):
RewriteCond %{HTTP_HOST} ^giccampers.com.au [NC]
RewriteRule ^(.*)$ http://blackseriescampertrailers.com.au/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^www.giccampers.com.au [NC]
RewriteRule ^(.*)$ http://blackseriescampertrailers.com.au/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^giccampers\.com\.au$ [OR]
RewriteCond %{HTTP_HOST} ^www\.giccampers\.com\.au$
RewriteRule ^/?$ "http\:\/\/www\.blackseriescampertrailers\.com\.au\/" [R=301,L]
RewriteCond %{HTTP_HOST} ^www.giccampers.com.au [NC]
RewriteRule ^(.*)$ http://blackseriescampertrailers.com.au/$1 [L,R=301]
redirect 301 /general-trailers/tipper-trailer/ http://blackseriescampertrailers.com.au/
Any help would be great!
use this
RedirectMatch 301 http://giccampers.com.au/general-trailers/tipper-trailer/ http://blackseriescampertrailers.com.au/
RedirectMatch 301 /general-trailers/tipper-trailer/ http://blackseriescampertrailers.com.au/
or
RedirectMatch 301 /tipper-trailer$ http://blackseriescampertrailers.com.au/
or
RedirectMatch 301 /general-trailers/tipper-trailer(.*)http://blackseriescampertrailers.com.au/

How to redirect one url to another url using .htaccess?

I am trying to redirect old url to new url but it is not redirecting.
Old url: http://domain.com/STACK/App/
New url: http://domain.com/stack-app
And my .htaccess file
Redirect 301 /STACK/App http://domain.com/stack-app
Thanks,
Try this code.
RewriteEngine on
RewriteRule http://domain.com/STACK/App/ /http://domain.com/stack-app [R=301,L]
For more help go to this link:--
http://edward-designer.com/web/htaccess-url-rewrite-simplified/
You can use this rule as your very first redirect rule in your root .htaccess:
RedirectMatch 301 ^/STACK/App/?$ /stack-app
Within the mod_rewrite section of your .htaccess file, add the following lines on the old site:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domain\.com\STACK\App\$
RewriteRule (.*)$ http://domain.com/stack-app/$1 [R=301,L]
</IfModule>
you can use the following code to redirect your website using .htaccess
RewriteEngine On
RewriteCond %{HTTP_HOST} !oldexample.com$ [NC]
RewriteRule ^(.*)$ http://www.newexample.com/$1 [L,R=301]
for non www version, you can use the following,
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !oldexample.com$ [NC]
RewriteRule ^(.*)$ http://newexample.com/$1 [L,R=301]

(.htaccess) 301 redirect with same url structure

I use 301 redirect from non-WWW to WWW domain! Works fine, but links are different.
Example:
from - ggkj.com/posts/godzilla/43
to - www.ggkj.com/index.php?view=posts&author=godzilla&aid=43
my .htaccess 301 redirect code:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteRule ^(.*) http://www.example\.com/$1 [L,R=301]
.htaccess code: http://pastebin.com/t3FA59uq
This is currently what i use on my website with success:
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{http_host} !^www.walterbax.ca [NC]
RewriteRule ^(.*)$ http://www.walterbax.ca/$1 [R=301,L]
EDIT: after looking into your question a little further I can tell you that your issue is improperly placed in the file. it must be hitting another rule that is rewriting it into URL variables.
I would start with a basic htaccess for the www. rule for testing and add to it from there.
Try this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^yourdomain.com$
RewriteRule ^(.*)$ http://www.yourdomain.com/$1 [R=301,L]

htaccess redirect problem

I have the following redirects in my htaccess file
Redirect 301 /communityed/index.htm http://www.domain.com/community_education
Redirect 301 /communityed http://www.domain.com/community_education
Redirect 301 /communityed/visit/index.htm http://www.domain.com/directions/
Redirect 301 /communityed/visit/pdfs/campus_map.pdf http://www.domain.com/images/uploads/pdf/campusmap.pdf
The first two redirects work perfectly, but the last two are not.
When to go domain.com/communityed/visit/index.htm it redirects it to domain.com/community_education/visit/index.htm, but it should go to domain.com/directions
Same thing the 4th redirect as well
http://domain.com/communityed/visit/pdfs/campus_map.pdf redirects to http://www.domain.com/community_education/visit/pdfs/campus_map.pdf, when it should really go to http://www.domain.com/images/uploads/pdf/campusmap.pdf
What am I doing wrong that the last 2 redirects are not working appropriately?
I imagine it has something to do w/ the first two redirects over-riding it, but I am fairly new to redirects.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?domain.com/.*$ [NC]
RewriteRule \.(gif|jpg|swf|flv|png)$ /feed/ [R=302,L]
Given that your redirects are not standard mod_rewrite looking, I'll assume that the matches are greedy, therefore you should change the odder from finest match to most general.
Redirect 301 /communityed/visit/pdfs/campus_map.pdf http://www.domain.com/images/uploads/pdf/campusmap.pdf
Redirect 301 /communityed/visit/index.htm http://www.domain.com/directions/
Redirect 301 /communityed/index.htm http://www.domain.com/community_education
Redirect 301 /communityed http://www.domain.com/community_education

Resources