I need to make some 301 redirects, like this:
/!go?ln=vector.dpsp&refs_=&m$id=27007&m$mid=27013 to http://domain.com/catalog/paz-320412-vektor.htm
/!go?ln=1223-15-57-1223.html&m$id=27148&m$mid=27940 to http://domain.com/catalog/gruzoviki/sedelnie-tyagachi-man
I tryed this code in .htaccess:
RewriteCond %{QUERY_STRING} ln=vector.dpsp&refs_=&m$id=27007&m$mid=27013
RedirectPermanent /!go http://domain.com/catalog/paz-320412-vektor.htm?
RewriteCond %{QUERY_STRING} ln=1223-15-57-1223.html&m$id=27148&m$mid=27940
RedirectPermanent /!go http://domain.com/catalog/gruzoviki/sedelnie-tyagachi-man?
But all this two urls are catched with first block and redirect goes always into http://domain.com/catalog/paz-320412-vektor.htm
What am I doing wrong?
There's a couple of things wrong here. First, you need to escape the $ symbols in your expression matching the %{QUERY_STRING}, it's a reserved symbol (for stuff like backreferences). The second thing is a RewriteCond doesn't affect a RedirectPermanent. Rewrite directives are part of mod_rewrite and Redirect directives are part of mod_alias, you can't tie a rewrite condition to a redirect, you need to use RewriteRule:
RewriteCond %{QUERY_STRING} ln=vector.dpsp&refs_=&m\$id=27007&m\$mid=27013
RewriteRule ^\!go$ http://domain.com/catalog/paz-320412-vektor.htm? [R=301,L]
RewriteCond %{QUERY_STRING} ln=1223-15-57-1223.html&m\$id=27148&m\$mid=27940
RewriteRule ^\!go$ http://domain.com/catalog/gruzoviki/sedelnie-tyagachi-man? [R=301,L]
Related
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.
My .htaccess file in my public_html has:
RewriteEngine on
RewriteRule ^testfolder/(.*)$ /$1 [R=301,L,NC]
RewriteRule ^(.*)$ testfolder/$1
According to http://htaccess.madewithlove.be/ which I am using to test output
when I enter http://www.mydomain.com/testfolder/aaa/bbb I get:
This rule was met, the new url is http://www.mydomain.com/aaa/bbb
Test are stopped, because of the R in your RewriteRule options. A redirect will be made with status code 301
Is there a way to perform the 301 redirect to make the url in the browser bar 'prettier'
while continuing to attempt to match patterns further down in the htacess file?
I have my codeigniter project in www.mydomain.com/testfolder. My first rule:
RewriteRule ^testfolder/(.*)$ /$1 [R=301,L,NC]
is to rewrite any navbar requests for a contoller which come in the form : www.mydomain.com/testfolder/controller/method to
www.mydomain.com/testfolder/controller/method. The second ruleis so that anyone www.mydomain.com/controller/method will use it turned internally into www.mydomain.com/testfolder/controller/method
Thank you in advance,
Bill
Addendum - my current .htaccess file:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+b1/(.*)\s [NC]
RewriteRule ^ /$1 [R=302,L]
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteRule ^(.*)$ /b1/$1 [L]
First of all your rules are flawed and will definitely cause infinite loop.
Reason being that 1st rule is externally redirecting /testfolder/aaa/bbb to /aaa/bbb.
Now after redirect 2nd rule will also kick in since you're just matching .* there and that will be internally forwarded to /testfolder/aaa/bbb/ and that rule will cause 1st rule to kick in once again and this will cause infinite loop.
Don't know your intent but you can use this code to avoid looping:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+b1/([^\s]+)\s [NC]
RewriteRule ^ /$1 [R=302,L]
RewriteRule ^(.*)$ /b1/$1 [L,P]
Once you verify it is working fine, replace R=302 to R=301. Avoid using R=301 (Permanent Redirect) while testing your mod_rewrite rules.
No. Once the client has been redirected a new request occurs and rewriting begins anew with the new URL.
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.
I have a simple htaccess redirect to send visitors from website.com/promotions to website.com/go/promotions. If I use the code below I get a redirect loop error:
RewriteEngine on
RewriteCond %{REQUEST_URL} !^go$
RewriteRule promotion/index.html go/promotion/index.html [R,L,NC]
The RewriteCond is supposed to prevent the loop by not redirecting if the request url contains "go" but it doesn't appear to be working.
In addition I'm trying to set up a regex so that promotion/anypage.html will be redirected to go/promotion/anypage.html. I tried the following code but it wouldn't match anything:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^go$
RewriteRule ^promotion/(.*)$ go/promotion/$1 [R,L,NC]
Does anybody have an clues as to why either of these blocks isn't working properly ?
Cheers
%{REQUEST_URL} isn't a var you can match against, you're probably thinking of %{REQUEST_URI}, but it starts with a leading slash so ^go will never match anything.
You just need a single rule:
RewriteEngine On
RewriteRule ^/?promotion/(.*)$ /go/promotion/$1 [R,L]
Or you can use mod_alias:
Redirect /promotion/ /go/promotion/
You're checking if the entire URL is just "go" on it's own, which it's not:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/?go/
RewriteRule promotion/index.html go/promotion/index.html [R,L,NC]
I've changed my URL structure to remove the category from the URLs of item pages.
I need some way of redirecting all the old pages to the new pages, using mod_rewrite. I have already a quite elaborate and messy .htaccess file.
What I'd need is to redirect:
http://example.com/category-name/item-name/ to this http://example.com/download-item-name/
Right now, I've got this in the htaccess file.
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} !^example.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301]
RewriteRule ^download-([^/]*)/screenshots/$ /screenshots.php?screenshots=$1 [L]
RewriteRule ^download-([^/]*)/screenshots/([^/]*)/$ /screenshots.php?screenshots=$1&shot=$2
#and now without trailing slash, so request will hit script which can do redirect.
RewriteRule ^download-([^/]*)/screenshots/([^/]*)$ /screenshots.php?screenshots=$1&shot=$2 [L]
RewriteRule ^feed/([^/]*)/$ /feed/?category=$1 [L]
RewriteRule ^download-([^/]*)-software/$ /category.php?category=$1 [L]
#this needs to stay above the below rule
RewriteRule ^download-([^/]*)/$ /software.php?shortname=$1 [QSA]
RewriteRule ^download-([^/]*)/([^/]*)/$ /software.php?shortname=$1&lang=$2 [QSA]
#and now without trailing slash, so request will hit script which can do redirect.
RewriteRule ^download-([^/]*)$ /software.php?shortname=$1 [QSA]
RewriteRule ^download-([^/]*)/([^/]*)$ /software.php?shortname=$1&lang=$2
RewriteRule ^downloading/([^/]*)/$ /download.php?downloading=$1
RewriteRule ^downloading/([^/]*)/([^/]*)/$ /download.php?downloading=$1&lang=$2 [L]
RewriteRule ^download/([^/]*)/([^/]*)/$ /send.php?key=$1&s=$2 [QSA]
RewriteRule ^FQ-([^/]*)/$ /content.php?page=$1 [L]
RewriteCond %{REQUEST_URI} ^(/[^/]+)(/.*)?$
RewriteCond %{DOCUMENT_ROOT}/categories%1/ -d
RewriteRule . /categories%{REQUEST_URI} [QSA]
Options -Indexes
FileETag None
ErrorDocument 404 http://example.com/404/
ErrorDocument 403 http://example.com/
#Some redirects
Redirect 301 /audio-video-players-codecs/zoom-player-max/ http://example.com/download-zoom-player-max/
Redirect 301 /audio-video-players-codecs/zoom-player-premium/ http://example.com/download-zoom-player-premium/
Redirect 301 /audio-video-players-codecs/zoom-player-pro/ http://example.com/download-zoom-player-pro/
Your .htaccess is messy because of simple things:
Rule #1: avoid mixing Redirect and RewriteRules directives. This will only confuse things. There's always a possibility to replace Redirect by RewriteRules directives.
Rule #2: if you don't need RewriteBase / (which is often the case, because the base is often /), don't write it. This will only confuse things. The shorter the better (that's what she tells me (just kiddin')).
Rule #3:: ^(.*)$ is the same as (.*). Here too, this makes things clearer.
Rule #4:: special rule for you: you can reduce by 2 almost all your RewriteRules using a thing like ^downloading/([^/]*)(/([^/]*))?/ which makes the second argument empty but match both one or two arguments (just valid in your Php that you can get an empty second argument). The shorter the better.
Then to answer your question: to redirect: http://example.com/category-name/item-name/ to this http://example.com/download-item-name/
RewriteRule ^category-([a-zA-Z]+)/item-([a-zA-Z]+)/ ^download-item-$2/ [QSA,R]
This should work
My favorite tool to check for regexp:
http://www.quanetic.com/Regex (don't forget to choose ereg(POSIX) instead of preg(PCRE)!)