I'm using magento and every url is acessible in two ways for example:
http://www.mysite.com/product-item, and
www.mysite.com/product-item?___store=default
I tried a regular 301 redirect in .htaccess but it won't redirect. Does anyone have the code that would automatically redirect all url's with the suffix ?___store=default back to the clean url's thus avoiding duplicate content issues?
Try:
RewriteEngine On
RewriteCond %{THE_REQUEST} \?___store=default
RewriteRule ^(.*)$ /$1? [L,R=301]
or
RewriteEngine On
RewriteCond %{QUERY_STRING} ___store=default
RewriteRule ^(.*)$ /$1? [L,R=301]
depending on how the query string is generated. The reason why a Redirect 301 probably didn't work is that you can't match against query strings using that statement.
Related
I've searched for this but the answers I've found seem to be around rewriting URLs or for redirecting lots of pages in an entire section and look confusing (lots of regex).
We need to add some 301 redirects and everything is working fine except for 13 URLs that contain query strings.
Is there a trick to redirecting these kinds of urls?
Redirect 301 /blogsearch/?akID%5B30%5D%5BatSelectOptionID%5D%5B%5D=39 /blogsearch/Coffee
Redirect 301 /blogsearch/?akID%5B30%5D%5BatSelectOptionID%5D%5B%5D=26 /blogsearch/Food
Redirect 301 /blogsearch/?akID%5B30%5D%5BatSelectOptionID%5D%5B%5D=27 /blogsearch/Wine
Redirect 301 /blogsearch/?akID%5B30%5D%5BatSelectOptionID%5D%5B%5D=29 /blogsearch/Travel
Redirect 301 /blogsearch/?akID%5B30%5D%5BatSelectOptionID%5D%5B%5D=37 /blogsearch/Hotel
Redirect 301 /blogsearch/?akID%5B30%5D%5BatSelectOptionID%5D%5B%5D=49 /blogsearch/Apartments
Any help would be much appreciated.
Cheers
Ben
Yes there's a trick. You can't use Redirect directive with query strings.
You could just do something like this with mod-rewrite. Just change the corresponding query string value for every individual link you have.
RewriteEngine On
RewriteCond %{QUERY_STRING} (.+)=39
RewriteRule ^blogsearch/?$ /blogsearch/Coffee? [L,NC,R=301]
RewriteCond %{QUERY_STRING} (.+)=26
RewriteRule ^blogsearch/?$ /blogsearch/Food? [L,NC,R=301]
RewriteCond %{QUERY_STRING} (.+)=27
RewriteRule ^blogsearch/?$ /blogsearch/Wine? [L,NC,R=301]
RewriteCond %{QUERY_STRING} (.+)=29
RewriteRule ^blogsearch/?$ /blogsearch/Travel? [L,NC,R=301]
RewriteCond %{QUERY_STRING} (.+)=37
RewriteRule ^blogsearch/?$ /blogsearch/Hotel? [L,NC,R=301]
RewriteCond %{QUERY_STRING} (.+)=49
RewriteRule ^blogsearch/?$ /blogsearch/Apartments? [L,NC,R=301]
I think I have read everything I can about htaccess rewrites and I still can't make heads or tails of whats going on. I re made a website for work and all is well except the last designer did some crazy php stuff and all the urls he used have ?=p(pagename) I want to rewite those to (pagename).php then redirect them to with a 301 I am able to get the 301 redirects works just can't figure out how to rewrite the ?p=(pagename) to (pagename).php
You want to be matching against the actual requests, then internally rewrite it back to the query string:
RewriteEngine On
# 301 redirect to php file
RewriteCond %{THE_REQUEST} \ /\?p=([^&\ ]+)&?([^\ ]*)
RewriteRule ^ /%1.php?%2 [L,R=301]
# internally rewrite to the query string
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\.php$ /?p=$1 [L,QSA]
You need to check the QUERY_STRING and then apply the rewrite rule
RewriteCond %{QUERY_STRING} ^p=(.*)$
RewriteRule ^(.*)$ http://mydomain.com/%1.php [R=301,L]
the ^p=(.*)$ checks for a query string that only has the one variable p=pagename, you will have to modify it if there will be any other variables in the query string it like p=pagename&id=15 etc
I'm trying to set 301 redirects on pages that have 'page=1' in the URL to stop duplicate content issues.
e.g.
http://www.domain.com/reviews/?page=1
to
http://www.domain.com/reviews/
I've tried all of the variations I can find and can't seem to get anything to work.
RewriteRule ^reviews(/)?page=1$ http://www.domain.com/reviews/ [L,R=301]
RewriteRule ^(.*)/?page=1 http://www.domain.com/reviews/ [R=301,L]
RewriteCond %{QUERY_STRING} page=1
RewriteRule ^reviews$ http://www.domain.com/reviews/ [R=301,L,NE]
None of these have worked. I'm not really sure what else to try.
There are multiple different sections of the site that I need to do this for:
reviews
news
videos
accessories
hardware
An overall solution to redirect all ?page=1 URLs to their relevant section would be best.
Use this code to redirect every URI with ?page=1 to one without the query parameter:
RewriteCond %{QUERY_STRING} ^page=1(&|$) [NC]
RewriteRule ^ %{REQUEST_URI}? [R=301,L]
Or else if you want to redirect ONLY /reviews URI then
RewriteCond %{QUERY_STRING} ^page=1(&|$) [NC]
RewriteRule ^(reviews)/?$ /$1? [R=301,L]
the question is already answered, i just would like to mention that your rules are not working because you didn't append a trailing ? to the new url in the rewrite rule
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
I am finding some problems in the htaccess of CMS with a 301 redirect.
When trying to solve canonical urls (redirecting site to www.site) I got the problem that I cannot log in in the back end (www.site/admin).
The htaccess condition is:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.site\.co.uk$
RewriteRule (.*) http://www.site.co.uk$1 [R=301,L]
I guess I need to include a expression that allows the URI /admin not to be redirected, but how?
Like this, for example:
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{HTTP_HOST} !=www.example.co.uk
RewriteCond %{REQUEST_URI} !^/admin
RewriteRule .* http://www.example.co.uk%{REQUEST_URI} [R=301,L]