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

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]

Related

Generic URL rewrite keeping query string

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.

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]

htaccess - allow certain parameters but redirect to 404 if other query string

Following problem:
I have a form on a page and want to allow certain parameters to enable form value overwriting. But if there is a questionmark in the url without any of the parameter I want to redirect to the 404 page.
But my regex doesn't work very well...
RewriteCond %{QUERY_STRING} !^(a=.*|b=.*|c=.*)
RewriteRule ^subfolder/(.*)$ - [R=404,L,NC]
But now my page only shows up if a parameter is set. http://example.com/subfolder/ does not work, but http://example.com/subfolder/?a=whatever works. I only want a redirect if there is a question mark without any my parameters. How to do that?
You can use this rule:
RewriteCond %{QUERY_STRING} .
RewriteCond %{QUERY_STRING} !(^|&)(a|b|c)= [NC]
RewriteRule ^subfolder/(.*)$ - [R=404,L,NC]
First condition RewriteCond %{QUERY_STRING} . makes sure that QUERY_STRING is non-empty.

Can't match RewriteRule

I need to do a very simple URL rewrite but my RewriteRule is not working.
I want this URL: http://myweb.com:8080/MySite/bla?bla
To become this: http://myweb.com:8080/MySite/index.php
My .htaccess file content is like this:
RewriteEngine On
RewriteRule bla\?bla index.php
It is located in "MySite" folder. I can do other url-rewriting rules with success but I got stuck whenever I need to write a rule with question mark inside.
What am I doing wrong?
You need to use the %{QUERY_STRING} rewrite condition for this.
RewriteEngine On
RewriteCond %{QUERY_STRING} ^bla$
RewriteRule ^/?bla$ index.php [NC,L]
Please, note that the ? in the rewrite rule is not there to match against the ? in the query string. That part is handled completely by %{QUERY_STRING}. The [NC] just makes the rule case-insensitive and [L] marks the rule as last.