htaccess to write URL - apache

I am trying to rewrite URL for users using htaccess file so that users can easily remember and make sure address bar stay cool.I accomplished to rewrite -
Rewrited URL
www.mysite.com/index.php?profile=2345
Actual URL wanted
www.mysite.com/profile/2345
.htaccess
RewriteRule ^profile/([0-9]+)/?$ index.php?profile=$1 [NC,L]
Unable to
**1. Multiple **
www.mysite.com/profile=2345&a=65&b=34&c=7&d=54
into
www.mysite.com/profile/2345/a/65/b/34/c/7/d/54
2. Is it possible to go further like?
profile.mysite.com/2345/a/65/b/34/c/7/d/54

1.
RewriteRule ^profile/([0-9]+)/a/([0-9]+)/b/([0-9]+)/c/([0-9]+)/d/([0-9]+)/?$ index.php?profile=$1&a=$2&b=$3&c=$4&d=$5 [NC,L]
2.
RewriteCond %{HTTP_HOST} ^profile\.mysite\.com$
RewriteRule ^([0-9]+)/a/([0-9]+)/b/([0-9]+)/c/([0-9]+)/d/([0-9]+)/?$ index.php?profile=$1&a=$2&b=$3&c=$4&d=$5 [NC,L]
3.
RewriteCond %{HTTP_HOST} ^([a-z]+)\.mysite\.com$
RewriteRule ^([0-9]+)/([abcd])/([0-9]+)/([abcd])/([0-9]+)/([abcd])/([0-9]+)/([abcd])/([0-9]+)/?$ index.php?%1=$1&$2=$3&$4=$5&$6=$7&$8=$9 [NC,L]
RewriteCond %{HTTP_HOST} ^([a-z]+)\.mysite\.com$
RewriteRule ^([0-9]+)/([abcd])/([0-9]+)/([abcd])/([0-9]+)/([abcd])/([0-9]+)/?$ index.php?%1=$1&$2=$3&$4=$5&$6=$7 [NC,L]
RewriteCond %{HTTP_HOST} ^([a-z]+)\.mysite\.com$
RewriteRule ^([0-9]+)/([abcd])/([0-9]+)/([abcd])/([0-9]+)/?$ index.php?%1=$1&$2=$3&$4=$5 [NC,L]
RewriteCond %{HTTP_HOST} ^([a-z]+)\.mysite\.com$
RewriteRule ^([0-9]+)/([abcd])/([0-9]+)/?$ index.php?%1=$1&$2=$3 [NC,L]
RewriteCond %{HTTP_HOST} ^([a-z]+)\.mysite\.com$
RewriteRule ^([0-9]+)/?$ index.php?%1=$1 [NC,L]

Related

htaccess rewrite log to multiple php files

I need help in solving the issue with my htaccess rewrite for multiple php file. No matter what I do the the last condition of vod movie is not working and always point to the first php file new.php . The first three are working fine because they point to same file. I have read in here that they shouldn't be ambiguous but I don't know how to do it or solve it. Please help me thanks.
RewriteEngine ON
RewriteBase /
RewriteCond %{HTTP_HOST} ^test\.com$ [NC]
RewriteRule ^live/([^/]*)/([^.]*)/([^/]*)\.m3u8/?$ new.php?code=$1&c=$3&type=m3u8 [NC,L]
RewriteCond %{HTTP_HOST} ^test\.com$ [NC]
RewriteRule ^live/([^/]*)/([^.]*)/([^/]*)\.ts/?$ new.php?code=$1&c=$3&type=ts [NC,L]
RewriteCond %{HTTP_HOST} ^test\.com$ [NC]
RewriteRule ^([^/]*)/([^.]*)/([^/]*)?$ new.php?code=$1&c=$3 [NC,L]
RewriteCond %{HTTP_HOST} ^test\.com$ [NC]
RewriteRule ^movie/([^/]*)/([^.]*)/([^/]*)\.mp4/?$ /vod.php?code=$1&c=$3 [NC,L]
Could you please try following, based on your shown samples only. Please do clear your browser cache before testing URLs.
RewriteEngine ON
RewriteBase /
RewriteCond %{HTTP_HOST} ^test\.com$ [NC]
RewriteRule ^live/([^/]*)/([^/]*)/([^.]*)\.m3u8/?$ new.php?code=$1&c=$3&type=m3u8 [NC,L]
RewriteCond %{HTTP_HOST} ^test\.com$ [NC]
RewriteRule ^live/([^/]*)/([^/]*)/([^.]*)\.ts/?$ new.php?code=$1&c=$3&type=ts [NC,L]
RewriteCond %{HTTP_HOST} ^test\.com$ [NC]
RewriteRule ^movie/([^/]*)/([^/]*)/([^/]*)/([^.]*)\.mp4/?$ vod.php?code=$1&c=$3 [NC,L]
RewriteCond %{HTTP_HOST} ^test\.com$ [NC]
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/?$ new.php?code=$1&c=$3 [NC,L]

Rewrite rules with multiple query string

i'm new on htaccess. I'm working on a website with multiple query strings.
I need to rewrite urls of different query strings like:
/path_to_website/file.php?product=var1&country=0&pag=1 to /path_to_website/var1/
/path_to_website/file.php?product=var1&country=0&pag=var2 to /path_to_website/var1/var2
/path_to_website/file.php?product=var1&country=var2&pag=1 to /path_to_website/var1/var2/
/path_to_website/file.php?product=var1&country=var2&pag=var3 to /path_to_website/var1/var2/var3
As you can see, the problem is between 2 and 3 rewrite because they have the same number of parameters (if the problem is this). I've done this on htaccess file:
###BLOCK 1#######
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^([^/]+)/$ /path_to_website/modules/file.php?product=$1&country=0&pag=1&rew=1 [L]
RewriteCond %{QUERY_STRING} ^(.*)=(.*)&country=0&pag=1$
RewriteCond %{QUERY_STRING} !^.*rew=1.*$
RewriteRule ^.*$ /path_to_website/%2/? [R=301,L]
###BLOCK 2#######
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^([^/]+)/([^/]+)/$ /path_to_website/modules/file.php?product=$1&country=0&pag=$2&rew=1 [L]
RewriteCond %{QUERY_STRING} ^(.*)=(.*)&country=0&(.*)=(.*)$
RewriteCond %{QUERY_STRING} !^.*rew=1.*$
RewriteRule ^.*$ /path_to_website/%2/%4/? [R=301,L]
###BLOCK 3#######
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^([^/]+)/([^/]+)/$ /path_to_website/modules/file.php?product=$1&country=$2&pag=1&rew=1 [L]
RewriteCond %{QUERY_STRING} ^(.*)=(.*)&(.*)=(.*)&pag=1$
RewriteCond %{QUERY_STRING} !^.*rew=1.*$
RewriteRule ^.*$ /path_to_website/%2/%4/? [R=301,L]
###BLOCK 4#######
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/$ /path_to_website/modules/file.php?product=$1&country=$2&pag=$3&rew=1 [L]
RewriteCond %{QUERY_STRING} ^(.*)=(.*)&(.*)=(.*)&(.*)=(.*)$
RewriteCond %{QUERY_STRING} !^.*rew=1.*$
RewriteRule ^.*$ /path_to_website/%2/%4/%6/? [R=301,L]
How can I fix this? Because everything works except when I search for product and country !=0 the engine stops on block 2.
Please someone help me because I'm stuck on this problem.
Thanks a lot!
Edit
Thanks #RavinderSingh13 for your answer. I tried with your rules, like this:
RewriteCond %{QUERY_STRING} ^$
############your rule###########
RewriteRule ^([\w-]+)/?$ file.php?product=$1&country=0&pag=1 [L]
RewriteCond %{QUERY_STRING} ^(.*)=(.*)&country=0&pag=1$
RewriteRule ^.*$ /path_to_website/%2/? [R=301,L]
The rewrite with this rules is path_to_website/product/, but the page doesn't work with "ERR_TOO_MANY_REDIRECTS". I tried also with ONLY your rule:
RewriteRule ^([\w-]+)/?$ file.php?product=$1&country=0&pag=1 [L]
In this case, the rewrite don't work at all, just print the query string. I also tried with the rew=1 key, like this:
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^([\w-]+)/?$ /aziende_agrarie/modules/risultato_ricerca.php?
prodotto=$1&provincia=0&pag=1&rew=1 [L]
RewriteCond %{QUERY_STRING} ^(.*)=(.*)&provincia=0&pag=1$
RewriteCond %{QUERY_STRING} !^.*rew=1.*$
RewriteRule ^.*$ /aziende_agrarie/%2/? [R=301,L]
The rewrite works with path_to_website/product, but when i change the page on 2, the browser url is path_to_website/product/?product=var1&country=0&pag=2. So I insert the rewrite for product/2/, and the rewrite for "file.php?product=var1&country=0&pag=var2" is:
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^([\w-]+)/([\w-]+)(?!=/)$ /aziende_agrarie/modules/risultato_ricerca.php?
prodotto=$1&provincia=0&pag=$2&rew=1 [L]
RewriteCond %{QUERY_STRING} ^(.*)=(.*)&provincia=0&(.*)=(.*)$
RewriteCond %{QUERY_STRING} !^.*rew=1.*$
RewriteRule ^.*$ /aziende_agrarie/%2/%4/? [R=301,L]
An so on. I miss something? Thanks a lot for your help :)
Based on your shown samples, could you please try following Rule sets in your .htaccess file. Also please make sure you clear your cache before testing your URLs.
RewriteEngine ON
##For file.php?product=var1&country=0&pag=1
RewriteRule ^([\w-]+)/?$ file.php?product=$1&country=0&pag=1 [L]
##For file.php?product=var1&country=0&pag=var2
RewriteRule ^([\w-]+)/([\w-]+)(?!=/)$ file.php?product=$1&country=0&pag=$2 [L]
##For file.php?product=var1&country=var2&pag=1
RewriteRule ^([\w-]+)/([\w-]+)/$ file.php?product=$1&country=$2&pag=1 [L]
##for file.php?product=var1&country=var2&pag=var3
RewriteRule ^([\w-]+)/([\w-]+)/([\w-]+)$ file.php?product=$1&country=$2&pag=$3 [L]
Edit
I had a GET request with form submit and href with custom paginator. I solved the problem. The problem was on href because i left the query string like "path_to_website/?product=var1&country=var2&page=1" etc. I resolved this replacing the query string href with "/product/country" for pag1 and so on for the other href. On the submit event, i left my custom block 1 and block 3 rules and for paginator href i wrote your rules for rewrite.

htaccess rewrite rule fails on https redirection

my htaccess look like this
RewriteEngine On
RewriteCond %{HTTP_HOST} !^localhost$
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteRule ^login/?(.*)$ login.php$1 [L]
RewriteRule ^register/?(.*)$ register.php$1 [L]
i want to redirect my pages login and redirect to https and so i added the code below
RewriteCond %{HTTPS} =on
RewriteCond %{REQUEST_URI} !^/(login|register)/?$
RewriteRule .*$ http://%{HTTP_HOST}%{REQUEST_URI} [L]
so when i type http://www.xyz.com/login/ it redirects to https://www.xyz.com/login/ everything was fine when i decided to redirect the pages to http which are not login or register and i wrote
RewriteCond %{HTTPS} =on
RewriteCond %{REQUEST_URI} !^/(login|register)/?$
RewriteRule .*$ http://%{HTTP_HOST}%{REQUEST_URI} [L]
after that i testted the url http://www.xyz.com/login/ again and it redirects me to https://www.xyz.com/login.php
any idea to redirect http://www.xyz.com/login/ to https://www.xyz.com/login/ and on that page when i click the link https://www.xyz.com/news/1/ redirect to http://www.xyz.com/news/1/
Try adding this right below the RewriteEngine On line:
RewriteCond %{HTTPS} off
RewriteRule ^(login|register)(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R]
RewriteCond %{HTTPS} on
RewriteRule !^(login|register) http://%{HTTP_HOST}%{REQUEST_URI} [L,R]
It also looks like you've pasted your rules wrong, the two blocks of rules are identical.

.htaccess with subdomains and GET requests

When a user accesses test.domain.co.uk, I would like to show them the page at channels/index.php?url=test where test can be anything.
I have this working with the following code:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.domain\.co\.uk$
RewriteCond %{HTTP_HOST} ^.+\.domain\.co\.uk$
RewriteRule ^(.*) %{HTTP_HOST}$1 [C]
RewriteRule ^(.+)\.domain\.co\.uk/? channels/index.php?url=$1 [L]
However, I would also like it to work if you called http://test.domain.co.uk/login/
And it would show you channels/index.php?url=test&page=login
I have tried a few ways and it just shows it as if it is ?url=test
Add
RewriteRule ^(.+)\.domain\.co\.uk(.+)$ channels/index.php?url=$1&page=$2 [L]
to the end of your rules. And add a $ to the pattern of your last rule:
RewriteRule ^(.+)\.domain\.co\.uk/?$ channels/index.php?url=$1 [L]
Then to keep from looping, add this to the top:
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]
You can use this rule:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.co\.uk$
RewriteRule ^((?!channels/index\.php).*)$ /channels/index.php?url=%1&page=$1 [L,QSA]

Redirect not working for complex rewrite rules (.htaccess)

I'm trying to make a redirect from a non-www version of the link to the www one. Its working fine for something like http://mywebsite.com but it fails for a request like http://mywebsite.com/artists/metallica/ or even a complex one. The whole .htaccess file is bellow. Any clues?
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*).html
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://mywebsite.com/$1/ [L,R=301]
RewriteRule ^artists/([^/-]+)-p([^/]+)/$ /artists.php?l=$1&p=$2 [QSA,L]
RewriteRule ^artists/([^/]+)/$ /artists.php?l=$1 [QSA,L]
RewriteRule ^submit/$ /submit.php [QSA,L]
RewriteRule ^users/$ /users.php [QSA,L]
RewriteCond %{THE_REQUEST} ^.*/index.php
RewriteRule ^(.*)index.php$ http://www.mywebsite.com/$1 [R=301,L]
Try this rule:
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
But make sure that you put this rule in front of those rules that just do an internal rewrite. Otherwise an already internally rewritten rule might get redirected externally.