htaccess: rewrite and redirect 301 - apache

I rewrited, by .htaccess, a category of dynamic url generated by a query string in this mode:
RewriteEngine On
RewriteRule ^id1-([^-]*)-id2-([^-]*)$ /page.php?id1=$1&id=$2 [L]
Now my rewrite works in right way and, for example, the following urls drive to the same page:
http://www.mysite.it/id1-01234-id2-56789
http://www.mysite.it/page.php?id1=01234&id2=56789
But now I want a redirect 301, from second type to first type, for all dynamic urls. For example:
from
http://www.mysite.it/page.php?id1=01234&id2=56789
to
http://www.mysite.it/id1-01234-id2-56789
The following way doesn't work:
RewriteEngine On
RewriteRule ^id1-([^-]*)-id2-([^-]*)$ /page.php?id1=$1&id=$2 [L]
RewriteCond %{QUERY_STRING} (^|&)id1=$1($|&)
RewriteCond %{QUERY_STRING} (^|&)id2=$2($|&)
RewriteRule ^page\.php$ /id1-id2? [L,R=301]
Where is the error?
can you help me please?

RewriteEngine On
RewriteCond %{THE_REQUEST} \s/page.php
RewriteCond %{THE_REQUEST} \?id1=(\w+)&id2=(\w+)\s
RewriteRule ^page.php /id1-%1-id2-%2? [NC,R=301,L]
RewriteRule ^id1-([^-]*)-id2-([^-]*)$ /page.php?id1=$1&id2=$2 [L]

Related

301 redirects dynamic URL's htacces

I'm having trouble figuring 301 redirecting this.
Old site has a structure like this for subcategories:
www.domain.com/?someid=1&pageload=serieslist&someparam=1
What I would like to do is redirect some of the subcategories to new categories 1:1, and then the rest to new domain root.
Will it work doing it like this, first listing all 1:1 redirects, and then a "catch all" for the rest?
RewriteEngine On
RewriteBase /
Redirect 301 /?someid=1&pageload=serieslist&someparam=1 http://www.newdomain.com/category1/
Redirect 301 /?someid=2&pageload=serieslist&someparam=1 http://www.newdomain.com/category2/
RewriteCond %{QUERY_STRING} ^pageload=serieslist$ [NC]
RewriteRule ^(.*)$ http://www.newdomain.dk/ [R=301,L]
You cannot use Redirect directive to match query string. Use it like this:
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} someid=1&pageload=serieslist&someparam=1 [NC]
RewriteRule ^$ http://www.newdomain.com/category1/? [L,R=302]
RewriteCond %{QUERY_STRING} someid=2&pageload=serieslist&someparam=1 [NC]
RewriteRule ^$ http://www.newdomain.com/category2/? [L,R=302]
RewriteCond %{QUERY_STRING} (^|&)pageload=serieslist(&|$) [NC]
RewriteRule ^ http://www.newdomain.dk/? [R=301,L]

Auto URL Rewrite with .htaccess

I am very newbie in mod-rewrite with .htaccess and i need some help to make this working.
For example, i have these two urls:
www.example.com/categories/example1
www.example.com/categories/example2
And i want to auto rewrite it as:
www.example.com/mypage/?cat=example1
www.example.com/mypage/?cat=example2
Any way to rewrite this with .htaccess?
I already tried this but still not working...
RewriteEngine On
RewriteBase /
RewriteRule ^example1([^/]*)$ mypage/?cat=$1 [L]
RewriteCond %{THE_REQUEST} ^(GET|POST|HEAD|TRACE)\ /mypage/
RewriteCond %{QUERY_STRING} name=([^\&]*)
RewriteRule ^mypage/$ categories/%1.php? [R,L]
You want the "example1" bit to be "categories" and you don't want to redirect to end with ".php". So something like:
RewriteEngine On
RewriteBase /
RewriteRule ^categories/([^/]*)$ mypage/?cat=$1 [L,QSA]
RewriteCond %{THE_REQUEST} ^(GET|POST|HEAD|TRACE)\ /mypage/
RewriteCond %{QUERY_STRING} cat=([^\&]*)
RewriteRule ^mypage/$ categories/%1? [R,L]

How to do a specific condition for escaped_fragment with rewrite rule in .htaccess

I have urls like that /#!/page1, I redirect them with :
RewriteCond %{QUERY_STRING} ^_escaped_fragment_=%2F(.*)$
RewriteRule ^$ /seo/%1.html [QSA,L]
So /#!/page1 read as /_escaped_fragment_=%2Fpage1 is redirected to /seo/page1.html
It's work perfectly however I want to redirect the home /#!/ to /seo/index.html which actually redirect to /seo/.html
How can I do that?
Thanks
I fixed it with:
RewriteCond %{QUERY_STRING} ^_escaped_fragment_=%2F$
RewriteRule ^$ /seo/index.html [QSA,L]
RewriteCond %{QUERY_STRING} ^_escaped_fragment_=%2F(.*)$
RewriteRule ^$ /seo/%1.html [QSA,L]

.htaccess redirect losing query string?

I have a .htaccess that looks like this:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteRule ^([^.]+)/?$ index.php?query=$1 [L]
I was hoping that would make all non-www urls redirect to the www version, as well as giving a tidy query string on the end. So these urls:
http://site.com/index.php?query=test/page
http://www.site.com/index.php?query=another/test/page
Would both redirect to:
http://www.site.com/test/page
http://www.site.com/another/test/page
Respectively.
However, everything is just redirecting to:
http://www.site.com
What am I doing wrong?
RewriteRule ignores the query string in the url. So you will need to change your rules as follows:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L,QSA]
RewriteCond %{HTTP_HOST} ^www\.
RewriteRule ^/index[.]php$ /%{QUERY_STRING} [L]
Make sure you include the QSA flag on the first rule

.htaccess redirect 301 using rewrite rule

I'm trying to beautify some urls. I configured the htaccess file so my urls are changed:
old url: http://mysite.com/index.php?id=45tye4
new url: http://mysite.com/45tye4
I want now to permanently redirect old urls to new urls. This is what I try with no luck:
RewriteRule ^index.php?id=(.*)$ $1 [R=301,L]
The main problem seems to be the '?' in the url. When I try the same url without ? the redirect works. I also tried other variants with no luck:
RewriteRule ^index.php\?id=(.*)$ $1 [R=301,L]
RewriteRule ^index.php[\?]id=(.*)$ $1 [R=301,L]
Update:
I added the redirection according to anubhava instructions. The redirection works, but unfortunately I get into a redirect loop. I thought [L] flag should solve the redirection loop, but it doesn't.
RewriteCond %{QUERY_STRING} ^id=(.*)$
RewriteRule ^index\.php/?$ /%1? [R=301,L]
RewriteRule ^(.*)$ index.php?id=$1 [L]
RewriteRule matches only REQUEST_URI. You have to use RewriteCond to match Query string
Try this code:
Options +FollowSymlinks -MultiViews
RewriteEngine on
RewriteCond %{THE_REQUEST} ^GET\s/+index\.php [NC]
RewriteCond %{QUERY_STRING} (^|&|\?)id=(.*)(&|$) [NC]
RewriteRule . /%2? [R=301,L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php?id=$1 [L]
This will redirect old URI of /index.php?id=45tye4 to new URI: /45tye4