.htaccess Rewrite GET-Data into GET-Data - apache

i want to rewrite http://192.168.2.101/game.php?village=567919&screen=overview into http://192.168.2.101/index.php?dir=game&page=overview&village=567919 can anyone tell my why this is not working well and how i do it right?
my .htaccess code
RewriteEngine On
RewriteRule ^$ index/
RewriteRule ^(.*)/$ index.php?dir=main&page=$1 [L,QSA]
RewriteRule ^game.php?village=(.*)&screen=(.*)$ index.php?dir=game&page=$2&village=$1 [L,QSA]

You have to use %{QUERY_STRING} to parse query string parameters.
Replace your code by this one in your htaccess
RewriteEngine on
RewriteCond %{QUERY_STRING} ^village=([^&]+)&screen=([^&]+)$
RewriteRule ^game\.php$ /index.php?dir=game&page=%2&village=%1 [L]
RewriteRule ^(.+)/$ /index.php?dir=main&page=$1 [L]
RewriteRule ^$ /index.php [L]

Related

Htaccess with multiple rewrite rules

I have a problem with my rewrite rule in htaccess file. This is my code:
#BLOCK 1#
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^([^/]+)/$ path_to_website/file.php?product=$1&country=0&pag=pag-1&rew=1 [L]
RewriteCond %{QUERY_STRING} ^(.*)=(.*)&country=0&pag=pag-1$
RewriteCond %{QUERY_STRING} !^.*rew=1.*$
RewriteRule ^.*$ /path_to_website/%2/? [R=301,L]
#BLOCK 2#
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^([^/]+)/([^/]+)/$ path_to_website/file.php?product=$1&country=$2&pag=pag-1&rew=1 [L]
RewriteCond %{QUERY_STRING} ^(.*)=(.*)&(.*)=(.*)&pag=pag-1$
RewriteCond %{QUERY_STRING} !^.*rew=1.*$
RewriteRule ^.*$ /path_to_website/%2/%4/? [R=301,L]
#BLOCK 3#
RewriteRule ^([\w-]+)/?$ path_to_website/file.php?product=$1&country=0&pag=pag-1 [L]
RewriteRule ^([\w-]+)/([\w-]+)(?!=/)$ path_to_website/file.php?product=$1&country=0&pag=$2 [L]
RewriteRule ^([\w-]+)/([\w-]+)/$ path_to_website/file.php?product=$1&country=$2&pag=pag-1 [L]
RewriteRule ^([\w-]+)/([\w-]+)/([\w-]+)$ path_to_website/file.php?product=$1&country=$2&pag=$3 [L]
The first two block comes from submit form and the third block comes from an href.
I want to add the following rules without conflict with the other rules:
RewriteRule ^([\w-]+)/([\w-]+)/([\w-]+)$ path_to_website/file2.php?product=$1&country=$2&product-details=$3 [L]
edit
Solved the proble, i just add another parameter to the rewrite for details-page:
RewriteRule ^([\w-]+)/([\w-]+)/([\w-]+).([\w-]+)$ path_to_website/file2.php?product=$1&country=$2&product-details=$3&place-holder=$4 [L]
Solved the problem, i just add another parameter to the rewrite for details-page:
RewriteRule ^([\w-]+)/([\w-]+)/([\w-]+).([\w-]+)$ path_to_website/file2.php?product=$1&country=$2&product-details=$3&place-holder=$4 [L]
Thanks to all :)

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: How to rewrite all urls to index.php excepting urls to one directory

I'm trying to rewrite all urls to index.php excepting for urls inside localhost/src/...
This is the htaccess I've been using so far:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/index\.php$
RewriteRule ^ /index.php [L]
UPDATE:
Nevermind I just came up with a solution:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/src
RewriteCond %{REQUEST_URI} !^/index\.php$
RewriteRule ^ /index.php [L]
However I would still like to know if there is a better way of doing it
Posting the update as an answer:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/src
RewriteCond %{REQUEST_URI} !^/index\.php$
RewriteRule ^ /index.php [L]

Swap 2 letter part of a url in htaccess file

Below is my htaccess file (complete). What I need to do is rewrite it so that images for say /uk/images/file.jpg become /images/uk/file.jpg. Although its not erroring its not working either. Am I missing something?
RewriteEngine on
RewriteRule ^/([A-Za-z]{2})/images/(.*)$ /images/$1/$2 [L]
RewriteCond $1 !^(index\.php|css|jscript|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
Try :
RewriteEngine on
RewriteRule ^/?([A-Za-z]{2})/images/(.*)$ /images/$1/$2 [NC,L]
RewriteCond %{REQUEST_URI} !^/images/([^/]+)/ [NC]
RewriteCond $1 !^(index\.php|css|jscript|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]

.htaccess redirect for all files but with exceptions

I know this has been asked thousands times, but I've been googling for three hours without any result. So I'm asking here. I'm creating a website. All the content is in the folder /subfolder. Now I want to redirect all the requests to another domain (let's say domain.com), at the exception of the files that I actually use. It may seem weird but it makes sense in my situation. So what I have at the moment is
RewriteEngine on
RewriteRule ^account$ account.php
RewriteRule ^home$ myhome.php
RewriteRule ^options$ options.php
RewriteRule ^login$ login.php
RewriteRule ^links$ editlinks.php
RewriteRule ^help$ howto.php
RewriteRule ^$ index.php
RewriteRule ^forgot$ forgot.php
RewriteRule ^r$ redirect.php
RewriteRule ^r/(.*)$ redirect.php?id=$1
RewriteCond %{REQUEST_URI} !(login.php|login)
RewriteRule (.*) external.php?parameter=$1 [L]
How can I do this ? the above code always redirects me to domain.com.
Thank you in advance
Make sure to place redirect just below RewriteEngine On and use THE_REQUEST instead of REQUEST_URI like this:
DirectoryIndex index.php
RewriteEngine on
RewriteCond %{THE_REQUEST} !/media [NC]
RewriteCond %{THE_REQUEST} !/(login|signup)(\.php)?[?\s] [NC]
RewriteRule (.*) http://domain.com/$1 [R=301,L]
RewriteRule ^home$ myhome.php [L]
RewriteRule ^links$ editlinks.php [L]
RewriteRule ^help$ howto.php [L]
RewriteRule ^(account|login|options|forgot)/?$ $1.php [L,NC]
RewriteRule ^r$ redirect.php [L]
RewriteRule ^r/(.+)$ redirect.php?id=$1 [L,QSA]