Auto URL Rewrite with .htaccess - apache

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]

Related

Htaccess not redirecting to new base

I would like to redirect anything that does not begin with /X, to /X, and, I'm trying to do this by using .htaccess with this content:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^(?!/X)
RewriteRule ^(.*)$ /X/$1
What am I missing?
EDIT:
I've also tried:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/X
RewriteRule ^(.*)$ /X/$1 [L]
and using this tool seems like it should work, but for example www.site.com/style.css
It's now working...
Have it like this:
RewriteEngine On
RewriteCond %{THE_REQUEST} !\s/+alsiniga/X[/?\s] [NC]
RewriteRule ^ /alsiniga/X%{REQUEST_URI} [L,R=301,NE]

htaccess: rewrite and redirect 301

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]

htaccess Redirect a Specific Directory URLs to another different URL?

With .htaccess how to redirect from:
http://apple.example.com/old_folder
http://apple.example.com/old_folder/
http://apple.example.com/old_folder/bla/bla/bla?a=b&etc
whatever depths under apple.example.com/old_folder --> to:
http://banana.example.com/new_folder/
.. only?
I used following code, but not working:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^apple.example.com/old_folder$
RewriteRule ^(.*)$ http://banana.example.com/new_folder/ [R,L]
You cannot match URI in RewriteCond %{HTTP_HOST} condition.
This rule should work from root .htaccess of apple.example.com:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^apple\.example\.com$ [NC]
RewriteRule ^old_folder(/|$) http://banana.example.com/new_folder/? [R,L]
EDIT: OR else use this rule in /old_folder/.htaccess:
RewriteEngine on
RewriteRule ^ http://banana.example.com/new_folder/? [R,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