htaccess 301 redirect with URL rewrite - .htaccess

I would need some expert advice on how to make a proper 301 URL redirect using htaccess. I already have a rewrite rule and need to also add a redirect. This is the rewrite rule:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^results/(case1|case2|case3)/([^/]*)/([^/]*)/([^/]*)/([^/]*)/([^/]*)/([^/]*)/$ /results.php?v1=$1&v2=$2&v3=$3&v4=$4&v5=$5&v6=$6&v7=$7 [L]
What should I add in the rule above to make a successful redirect from:
http://www.website.com/results.php?v1=case2&v2=a&v3=b&v4=c&v5=d&v6=e&v7=f
to:
http://www.website.com/results/case2/a/b/c/d/e/f/

You need an additional rule to redirect your url, add the following bellow RewriteEngine on line :
RewriteCond %{THE_REQUEST} /results\.php\?v1=(case1|case2|case3)&v2=([^&]+)&v3=([^&]+)&v4=([^&]+)&v5=([^&]+)&v6=([^&]+)&v7=([^&\s]+)\sHTTP [NC]
RewriteRule ^ /results/%1/%2/%3/%4/%5/%6/%7? [NC,L,R=301]

Related

How to redirect rewritten url

URL is rewritten using following rule.
RewriteEngine on
RewriteRule ^([a-zA-Z]+)/$ category?id=$1
to change
http://localhost/newsite/category?id=home
to following structure
http://localhost/newsite/home/
Now I tried to redirect, newsite/category?id=home to newsite/home/, to make clean URL using redirect rule, such as 301, redirect, but it doesn't work.
You can use this set of rules in newsite/.htaccess:
RewriteEngine on
RewriteBase /newsite/
RewriteCond %{THE_REQUEST} /category(?:\.php)?\?id=([^\s&]+) [NC]
RewriteRule ^ %1? [R=301,L]
# internal forward from pretty URL to actual one
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([\w-]+)/$ category.php?id=$1 [L,QSA]
You missed the .php in the rewrite statement
RewriteRule ^([a-zA-Z]+)/$ category?id=$1
change to
RewriteRule ^([a-zA-Z]+)/$ category.php?id=$1

Redirect urls with question mark and other parameters in htaccess

I want to redirect pages like:
/category-name/post-name.html?id=1234
To:
/category-name/1234-post-name.html
How can do this using htaccess?
What I have tried:
RewriteCond %{QUERY_STRING} ^id=(.*)$
RewriteRule ^([^/]+)/([^.]+)\.html$ /$1/%1-$2\.html [L,R=301]
But it is a continuous redirect.
You can use these rules in your site root .htaccess:
RewriteEngine On
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+([\w-]+)/([\w-]+)\?id=([^\s&]+) [NC]
RewriteRule ^ /%1/%3-%2? [R=301,L,NE]
# internal forward from pretty URL to actual one
RewriteRule ^([\w-]+)/([^-]+)-([\w-]+)/?$ $1/$3?id=$2 [L,QSA]
Try this
Make sure the url is root url
Example:-
www.foo.com/category-name/post-name.html
It will only work if the project url is same as that of the above.
www.foo.com/blog/category-name/post-name.html
This won't work you need to update the RewriteBase url accordingly l.
This is the conditions
RewriteEngine On
RewriteBase /
RewriteCond %(REQUEST_FILENAME) !-f
RewriteCond %(REQUEST_FILENAME) !-d
RewriteRule here
RewriteRule ^category-name/([0-9]+)-post-name$ /category-name/post-name.html?id=$1 [L]
This should work..

How to do a 301 redirect for an index.php?dynamic=url

I have 1 old index.php?etc_etc URL I would like to forward to a specific URL, but the 301 redirect is not working.
My rewrite conditions are:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\.html$ /$1 [L,R=301]
I have been using this format to rewrite URLs.
redirect 301 /index.php?page=shop.product_details&flypage=flypage.tpl&product_id=948&category_id=114&option=com_virtuemart&Itemid=69 /739619-5004s-garrett-gtp38r-ball-bearing-turbo-kit-99-5-03-7-3l-power-stroke
This does not work. I imagine because Magento utilizes the index.php for the home page.
How would I permanently redirect that URL to a new one.
You can't match against the query string in a Redirect directive. You need to use mod_rewrite and match against the %{QUERY_STRING} variable in a condition. So right below RewriteEngine On, add:
RewriteCond %{QUERY_STRING} ^page=shop.product_details&flypage=flypage.tpl&product_id=948&category_id=114&option=com_virtuemart&Itemid=69
RewriteRule ^index\.php$ /739619-5004s-garrett-gtp38r-ball-bearing-turbo-kit-99-5-03-7-3l-power-stroke? [L,R=301]

How do I rewrite a single parameter using htaccess?

I have URLs in the following format:
http://www.mysite.com/index.php?l=accommodation/lodge/some-place
I would like to use htaccess to rewrite it to this format:
http://www.mysite.com/accommodation/lodge/some-place
How would I do that? I started trying to do it with drupal's htaccess file and got something like this:
# Rewrite URLs of the form 'x' to the form 'index.php?q=x'.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ index.php?l=$1 [L,QSA]
Problem is, if I enter the original index.php url, it still shows. It must redirect to the short version. In other words, this:
http://www.mysite.com/index.php?l=accommodation/lodge/some-place
Must forward to this:
http://www.mysite.com/accommodation/lodge/some-place
Thoughts?
Insert this additional rule above your rule:
RewriteCond %{THE_REQUEST} \s/+index\.php\?l=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=302,L]
This will externally redirect http://www.mysite.com/index.php?l=accommodation/lodge/some-place to http://www.mysite.com/accommodation/lodge/some-place

HTACCESS and redirect 301

I want to make a redirect 301 via htaccess but for some reason it's doesn't works because I have more rules in htaccess, but if I remove them and leaves just the redirect it's works.
any idea?..
Options +FollowSymLinks
RewriteEngine On
ErrorDocument 404 error.php
RewriteRule ^sitemap.xml sitemap.php
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule .* - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^(.*)$
RewriteRule (.*) seoPage.php?w=%{REQUEST_URI}
Put the redirect to the first place.
In htaccess the first matching rule workes.
So, there may be another rule overwriting this one.
It does not seems to me you are doing 301 in whole document. 301 should look like this
RewriteCond %{REQUEST_URI} ^(.*)$
RewriteRule (.*) seoPage.php?w=%{REQUEST_URI} [L,R=301]
With emphasis on R=301 . Check this page. There in detail described what each part of rewriting rule mean. Alternatively you can check documentation for .htaccess files.
Also lvil has a good point.

Resources