.htaccess redirect 301 using rewrite rule - apache

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

Related

htaccess rewrite if url doesnt have a query string

I have a htaccess that rewrites to /login if conditions aren't met.
It's working fine for urls without query_string.
However I have no clue how to include /reset?query_string to accepted conditions. And want to exclude /reset
/reset?6tdsBMxpeeJEDUvYzwKmLzIusLYLjgMtIj
RewriteEngine On
RewriteBase /index.php
RewriteCond %{REQUEST_URI} !^/login$
RewriteCond %{REQUEST_URI} !^/forgotten$
RewriteRule ^([^\.]+)$ /login [R=301,L,QSD]
RewriteRule ^(/)?$ /login [R=301,L]
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Here is a modified version of your .htaccess that excludes /reset?query:
RewriteEngine On
# /reset with query string
RewriteCond %{QUERY_STRING} .
RewriteRule ^(reset)/?$ $1.php [L,NC]
RewriteCond %{REQUEST_URI} !^/(forgotten|login)$ [NC]
RewriteRule ^([^.]*)$ /login [R=301,L,QSD]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^.]+)$ $1.php [L]
Make sure to test it after clearing your browser cache.
With your shown samples and attempts, please try following htaccess rules file. Make sure to clear your browser cache before testing your URLs.
Also I am making use of apache's variable named THE_REQUEST by which we can handle both uri as well as query string.
RewriteEngine ON
RewriteBase /
##Rules for handling rest uri without query string.
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^reset/?$ /login? [R=301,L,NC]
##Rules for reset with uri and query string.
RewriteCond %{THE_REQUEST} \s/reset\?\S+\s [NC]
RewriteRule ^ /reset? [R=301,NC]
##Rule for login page's backend rewrite.
RewriteRule ^login/?$ login.php [QSA,NC,L]
##Rule for forgotten page's backend rewrite.
RewriteRule ^forgotten/?$ forgotten.php [QSA,NC,L]
##Rules for non-existing pages should be served with index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [QSA,L]

How to rewrite .php to .html using .htaccess rules?

I tried and failed.
Original URL
https://www.sitename.com/user/login/index.php
https://www.sitename.com/user/dashboard/index.php
URL that the browser should show
https://www.sitename.com/user/login.html
https://www.sitename.com/user/dashboard.html
This is what I tried and it doesnt work
RewriteEngine on
RewriteBase /
RewriteCond %{THE_REQUEST} (.*)\.php
RewriteRule ^(.*)\.php http://%{HTTP_HOST}/user/$1.html [R=301,L]
RewriteCond %{THE_REQUEST} (.*)\.html
RewriteRule ^(.*)\.html http://%{HTTP_HOST}/user/$1.php [L]
Could you please try following. Written and tested with your shown samples. Please make sure you clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteCond %{REQUEST_URI} ^/([^/]*)/([^.]*)\.html/?$ [NC]
RewriteRule ^(.*)$ %1/%2/index.php [L]

How to ignore URL at the end of URL

I have a URL
original: https://www.example.com/video/12345/name-of-the-video/
Sometimes users or web crawlers write the URL differently:
https://www.example.com/video/12345/name-of-the-video/65485/
https://www.example.com/video/12345/
https://www.example.com/12345/
I want all 3 URLs to redirect (forward) to the original:
https://www.example.com/video/12345/name-of-the-video/
My current htaccess rule is:
RewriteRule ^video/([0-9]+)/([^/]+)/$ view_video.php?id=$1&dir=$2 [L,QSA]
RewriteRule ^video/([0-9]+)/$ view_video.php?id=$1&dir=fake [L,QSA]
Could you please try following, written based on your shown samples. Please make sure you clear your browser cache before testing your URLs.
RewriteEngine ON
##To rewrite URL https://www.example.com/12345/
RewriteRule ^([0-9]+)/?$ view_video.php?id=$1&dir=fake [NC,L]
##To rewrite URL https://www.example.com/video/12345/
RewriteRule ^video/([0-9]+)/?$ view_video.php?id=$1&dir=fake [NC,L]
##To rewrite URL https://www.example.com/video/12345/name-of-the-video/65485/
RewriteRule ^video/([0-9]+)/([\w-]+)/[0-9]+/?$ view_video.php?id=$1&dir=$2 [R=301,NC]
RewriteCond %{REQUEST_URI} ^/view_video\.php [NC]
RewriteCond %{QUERY_STRING} id=([^&]*)&dir=(.*) [NC]
RewriteRule ^(.*) video/%1/%2 [R=301,L]
##To rewrite URL https://www.example.com/video/12345/name-of-the-video/
RewriteRule ^video/([0-9]+)/([\w-]+)/?$ view_video.php?id=$1&dir=$2 [NC,L]

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]

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]