Generic URL rewrite keeping query string - regex

What is the proper Apache mod_rewrite rule to change any
mydomain.com/?var1=val1&var2=val2&...&mode=app_mode&varN=valN&...
type query to
mydomain.com/app_mode/?var1=val1&var2=val2&...&varN=valN&...
(If the mode variable is not present in the QUERY_STRING, the URL should be kept as is.)

You can use this rule in your root .htaccess:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^(.*&)?mode=([^&]+)(?:&(.*))?$ [NC]
RewriteRule ^/?$ /%2/?%1%3 [L,R=302,NE]
This allows mode parameter to be anywhere in the query string.

Related

.htaccess mod_rewrite skips capture group when group is empty

I'm using .htaccess to rewrite URLs with two groupings.
My .htaccess:
RewriteRule ^tips/([0-9]*)/?([0-9]*)?/?(.+)?$ /tips.php?item=$1&id=$2 [L,QSA]
It works correctly when there are two numbers are in the URL, but when one is empty, it skips the capture group entirely.
example.com/tips/1/2/abc -> /tips.php?item=1&id=2 (EXPECTED)
example.com/tips//2/abc -> /tips.php?item=2&id= (UNEXPECTED)
/tips.php?item=&id=2 (WHAT WAS EXPECTED)
I've put this into a few regex/htaccess testers but they all seem to say it should be working as I expected.
(This of course is an unusual URL, but I want to pass it to PHP so I can handle the error in PHP.
You may use this rewrite rule with a THE_REQUEST variable:
Options -MultiViews
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/+tips/(\d*)/(\d*)/ [NC]
RewriteRule ^ tips.php?item=%1&id=%2 [L,QSA]
mod_rewrite engine converts multiple // into a single / in RewriteRule pattern therefore we need to use THE_REQUEST here that matches against original request received in Apache.
With your shown samples, could you please try following.
Options -MultiViews
RewriteEngine ON
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^tips/(\d+)/(\d+)/?$ tips.php?item=$1&id=$2 [L,QSA]
This will look for non existing files/directories and rewrite them to php file with parameters, like #anubhava sir mentioned multiple /slashes will be converted to single / by rewrite rule so this should work for both of your mentioned cases.

RewriteRule when there is ?id=

I try to write a RewriteRule in the .htaccess but I have problems
I try to redirect from:
blog/entrada.php?id=2
To:
/blog/3D-touch
This is one of the multiple things I tried and does not work:
RewriteRule ^blog\/entrada\.php\?id=2$ /blog/3D-touch [L,R=301]
What is wrong with my Rule. How to redirect effectively?
Thanks
Querystring is not part of match in RewriteRule directive, to redirect query strings, you need to use RewriteCond one of the following options :
option 1
RewriteEngine on
RewriteCond %{THE_REQUEST} /blog/entrada\.php\?id=2 [NC]
RewriteRule ^ /blog/3D-touch? [NC,L,R]
option 2
RewriteEngine on
RewriteCond %{QUERY_STRING} ^id=2$ [NC]
RewriteRule ^blog/entrada\.php$ /blog/3D-touch? [NC,L,R]
We use an empty question mark ? at the end of the target url to discard the old query strings, otherwise these query strings get appened to the target url by default.
Change the R to R=301 if you want to make the redirection permanent.
use from this code
RewriteRule blog/(.*) blog/entrada.php?id=$1
this code will redirect all urls which have blog/ to blog/entrada.php and put after value of blog/ to $_GET['id']
you should have following code n the top location of your htaccess file
Options +FollowSymLinks
RewriteEngine on

Rewrite domain name/url with htaccess

I need to rewrite the following url:
mydomain.com/dashboard/index.php?user=john-dow
to
john-dow.mydomain.com/dashboard/index.php
So I want to take the query string value and place it at the beginning of my domain followed by a dot. The goal is to create a customized user url, so it looks very friendly to users. But actually, I don't know how can I do that. Is it possible by .htaccess? If yes, how?
Use in your dashboard/.htaccess:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^(.+)\.mydomain\.com$ [NC]
RewriteCond %{QUERY_STRING} ^user=(.+)$
RewriteRule ^index\.php$ http://%1.mydomain.com/dashboard/index.php? [R,L]

How to RewriteRule WITH and WITHOUT parameters in htaccess?

I want to route all .php and .html files to a certain php file wether they have URL parameters or not.
Example:
www.mydomain.com/mysite.php?par1=test
www.mydomain.com/intro.html?nopar=1
Idea:
RewriteRule \.(?:php|html)$ work.php
You're pretty close, just add a condition to avoid rewriting if request is already for work.php:
RewriteEngine On
RewriteCond %{REQUEST_URI} !/work\.php$ [NC]
RewriteRule \.(?:php|html)$ work.php [L,NC]
Note that query string is automatically carried over to target URI.

apache rewrite rule for url's when no query is used

I have the following rewrite rule that I want to use when an user accesses the website like www.domain.com/home.php. It redirects him to the root.
RewriteRule ^home\.php / [R=301,L]
However, I don't want it to use when a query (domain.com/home.php?var1=999) is used. How make it only apply for urls without a query string?
Add rule condition with QUERY_STRING ( RewriteCond %{QUERY_STRING} ) before the rule.
http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html#rewritecond
http://wiki.apache.org/httpd/RewriteQueryString
Add a rewrite condition that checks that query string is empty.
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^home\.php / [R=301,L]