Url rewrite from .htaccess with Get variables - php

I am working in a project in PHP , I am not using MVC but keeping a single index.php files which changes it's content based on $_GET['module']
My current url format is
http://phpquiz.com/school/index.php?module=user_groups&mid=13&cmid=32
Now i want to change it to
http://phpquiz.com/school/index/user_groups/13/32
I used following script and was able to rewrite url but my site stopped working as it lost get variables.
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/school/index.php [NC]
RewriteCond %{QUERY_STRING} ^module=(.*)&mid=(.*)&cmid=(.*)
RewriteRule (.*) http://phpquiz.com/school/%1/%2/%3.asp? [R=301,L]
How can i do this by keeping get vatiables.

Try this:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/school/index.php [NC]
RewriteCond %{QUERY_STRING} ^module=([^&]+)&mid=([^&]+)&cmid=([^&]+)
RewriteRule ^ http://local.htaccess/school/index.php/%1/%2/%3.asp? [R=301,L]

You seem to be doing reverse. Have this rule:
RewriteEngine On
RewriteRule ^school/index\.php/([^/]+)/([^/]+)/([^/]+)\.asp$ /school/index.php?module=$1&mid=$2&cmid=$3 [NC,QSA,L]

Related

htaccess redirect query string to a path

So i have a htaccess rewrite that makes a query string to a path like so..
RewriteRule ^page/(.*)$ page.php?query=$1
This works fine and using the above i can access the page via both page.php?query= and /page/query/ but how can i make it so that if the page is accessed by the page.php?query= method, it automatically redirects to the path version?
I've tried the following but i don't think i wrote it correctly...
RewriteCond %{HTTP_HOST} ^www\.webaddress\.com\/page\.php\?query\=$
RewriteRule ^(.*)$ http://www.webaddress.com/page/$1 [R=301,L]
Any help to fix this would be appreciated. Thanks.
You can use the following
RewriteEngine On
#url redirection from old url to the new one
#redirect /page.php?query=foo to /page/foo
RewriteCond %{THE_REQUEST} /page.php\?query=([^\s]+) [NC]
RewriteRule ^.+$ /page/%1? [L,R]
#url rewiring from new url to the old one
# rewrite /page/foo to /page.php?query=foo
RewriteRule ^page/(.*)$ page.php?query=$1 [L]
Try it before your internal rewrite rule.
RewriteEngine On
RewriteCond %{REQUEST_URI} ^(.+)\.php
RewriteCond %{QUERY_STRING} ^(.+)\=(.+)
RewriteRule ^ http://%{HTTP_HOST}/%1/%2/%3? [R=301]

Subdomain URL Redirect dynamically. .htaccess

I want redirect url using .htaccess
like.
abc.website.com to website.com/folder/abc
http://abc.website.com to http://www.website.com/folder/abc
using .htaccess from PHP server
Use below rule,
RewriteEngine on
RewriteCond %{HTTP_HOST} !^(www|webmail|help|whm|root)
RewriteCond %{HTTP_HOST} ^(.+?)\.website.com
RewriteRule ^ http://www.website.com/folder/%1 [R=301,L]
Try putting this in your .htaccess file:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^abc.website.com
RewriteRule ^(.*)$ http://website.com/folder/abc/$1 [L,NC,QSA]
For a more general rule (that works with any subdomain, not just sub) replace the last two lines with this:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*)\.abc.website\.com
RewriteRule ^(.*)$ http://website.com/folder/%1/$1 [L,NC,QSA]
I hope it will work for you.

replacing Login URL to short URL with .htaccess

I have an URL as :
http://mydomain.com/site/?cmd=home
I want to change the above address to http://mydomain.com/home
i am using .htaccess file like this:
RewriteEngine on
RewriteRule ^([^/\.]+)/?$ /site?cmd=$1 [L]
I am not sure where is the problem?
Thanks in advance
The Rule works the other way around: you want to get from /site/?cmd=home to /home, but with your RewriteRule you are redirecting to /site?cmd= (Probably there is a mistake somewhere?)
If you want to redirect from /site/?cmd=home to /home, like the question states, use the following:
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/site
RewriteCond %{QUERY_STRING} ^cmd=(.*)$
RewriteRule ^(.*)$ /%1/? [R=302,L]
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{THE_REQUEST} /site\?cmd=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=301,L]
# internal forward from pretty URL to actual one
RewriteRule ^([^/.]+)/?$ /site?cmd=$1 [L]

.htaccess rewrite with HTTP_HOST and QUERY_STRING

I would like to set up a redirect to a new host with query string. I am struggling to get the query added to the end of the target url. Here is my example old url
http://sub.oldexample.com/folder1/page1?value=ABC123
I would like it to go to
http://newexample.com/folder3/page6?value=ABC123
I do not have a problem when a query is not involved, for example the following code works perfectly but is unsuitable
RewriteCond %{HTTP_HOST} ^sub.oldexample.com$
RewriteRule ^folder1/([A-Za-z][A-Za-z][A-Za-z][0-9][0-9][0-9])$ http://newexample.com/folder3/$1 [L,R=301,NC]
I have tried to include a query condition but it fails
RewriteCond %{HTTP_HOST} ^sub.oldexample.com$ [NC]
RewriteCond %{QUERY_STRING} ^(value=ABC123)
RewriteRule ^$ http://newexample.com/folder3/page6?%1 [L,R=301,NC]
I find the following link a perfect environment for testing a .htaccess rule as it gives the output url that is generated
http://htaccess.madewithlove.be/
Following rule should work for you:
RewriteCond %{HTTP_HOST} ^sub\.oldexample\.com$ [NC]
RewriteCond %{QUERY_STRING} (^|&)value=ABC123(&|$) [NC]
RewriteRule ^folder1/page1/?$ http://newexample.com/folder3/page6 [L,R=301,NC]
You don't need to copy QUERY_STRING as it will be automatically carried over to new URL.
Try this:
RewriteCond %{HTTP_HOST} ^sub.oldexample.com$ [NC]
RewriteCond %{QUERY_STRING} (.+)=(.+)
RewriteRule ^/?folder1/page1$ http://newexample.com/folder3/page6 [QSA,R=301]
Just try to use the [QSA] flag.

htaccess trouble rewriting language param

I'm having trouble rewriting the subfolder back to the file.
The redirect works but not the last rule.
Here's what I'm trying to do:
domain.com/file.php?lang=fr
would like to rewrite it back to
domain.com/fr/file.php
no matter what folder the files are in.
I'm only using one language which is french.
RewriteCond %{QUERY_STRING} ^lang=([A-Za-z-]+)/?$
RewriteRule ^(.*)\.php$ %1/$1.php? [NS,R=301,L]
RewriteRule ^fr/(.*)\.php/?$ $1.php?lang=fr&redirect=no [QSA,L]
my current htaccess file:
RewriteEngine On
RewriteBase /domain.com
RewriteCond %{QUERY_STRING} (^|&)lang=([A-Za-z-]+)(/?$|&)
RewriteCond %{QUERY_STRING} !(^|&)redirect=noneed($|&)
RewriteRule ^(.*)\.php$ %1/$1.php? [NS,R=301,L]
RewriteRule ^fr/(.*)\.php/?$ http://localhost/?$1.php?lang=fr&redirect=noneed [QSA,L]
The 1st rule is more powerful then second, so to solve your issue you have to write second at 1st place.
why not use a system like that:
RewriteRule ^(fr|en|es)/(accueil|home|bienvenida)/$ /index.php?lang=$1&act=index[QSA,L]
Try adding the RewriteBase option:
RewriteRule On
RewriteBase /
RewriteCond %{QUERY_STRING} ^lang=([A-Za-z-]+)/?$
RewriteRule ^(.*)\.php$ %1/$1.php? [NS,R=301,L]
RewriteRule ^fr/(.*)\.php/?$ $1.php?lang=fr&redirect=no [QSA,L]
(or if you don't want that option append a / at the beginning of all paths)
Also, your RewriteCond doesn't seem to handle the query string correctly, any additional parameters will cause your first rule to fail. Do this instead (I am still thinking):
RewriteRule On
RewriteBase /
RewriteCond %{QUERY_STRING} (^|&)lang=([A-Za-z-]+)(/?$|&)
RewriteCond %{QUERY_STRING} !(^|&)redirect=noneed($|&)
RewriteRule ^(.*)\.php$ %2/$1.php? [NS,R=301,L]
RewriteRule ^fr/(.*)\.php/?$ $1.php?lang=fr&redirect=noneed [QSA,L]

Categories