Rewrite using .htaccess if query string is empty - .htaccess

Might be a quick question for someone but it's not an area I have a huge amount of knowledge in.
I want to rewrite a URL using .htaccess if the query string is empty:
http://mydomain.com/js/file.js?foo=
So it rewrites to something like:
http://mydomain.com/js/error.js
Thanks in advance.

RewriteCond %{QUERY_STRING} ^foo=$
RewriteRule ^js/file.js$ /js/error.js [R=301,L]

Related

rewrite rule to convert php querystring to html

Please help me in writing correct .htaccess code to get the wanted result.
I have the following pages:
www.mydomain.com/?exam=/hp-certification/
www.mydomain.com/?exam=/cisco-certification/
www.mydomain.com/?exam=/oracle-exam-prep/
www.mydomain.com/?exam=/faq/
www.mydomain.com/?exam=/support/
And there are many more pages but you can notice that the fixed part is: www.mydomain.com/?exam=
Instead of using query string links, I want the following links to work as above pages:
www.mydomain.com/hp-certification/
www.mydomain.com/cisco-certification/
www.mydomain.com/oracle-exam-prep/
www.mydomain.com/faq/
www.mydomain.com/support/
and so on.
your link should be like this www.mydomain.com/?exam=hp-certification/ and the rewrite rule would like this in .htaccess file.
RewriteEngine On
RewriteRule ^([^/.]+)$ /?exam=$1 [L]
You can do that by using the following rules in your .htaccess file:
RewriteEngine On
RewriteCond %{QUERY_STRING} exam=(.+) [NC]
RewriteRule ^(.*)$ http://www.example.com/%1? [R=301,NC,L]
So what does the above do?
First, it will take the query exam= as a condition, if this condition is met then it will grab any version of the variable using (.+).
It will then rewrite the URL using 301 redirection to show http://www.example.com/anything/. The use of %1 is to grab the variable from exam= and then we use ? to stop the original query string from appearing on the end of the newly rewritten URL.
Make sure you clear your cache before testing this.

.htaccess rewrite URL with a question mark "?"

My aim is this URL:
component/users/?view=registration
To:
registration.html
the .htaccess is in the folder mysite of the website.
i tried this:
RewriteBase /mysite
RewriteRule ^component/users/?view=registration$ registration.html$ [R=301,L]
But i doesnt work...
When i try this:
RewriteRule ^component/users/_view=registration$ registration.html$ [R=301,L]
it works very well.
So how can i fix this problem with the question mark. I already read thats it is not a part of the URL (its appended). I have read that i have to use something like querystring, but i didn't really understand the syntax.
Maybe someone could write the solution of this problem? Would be awesome =)
You need to use %{QUERY_STRING} to capture the query string data:
RewriteCond %{QUERY_STRING} ^view=(.*)$
RewriteRule ^component/users/?$ %1.html? [R=301,L]
The above rule/condition will take the value of the query string view and use it to form your redirect if the path component/users matches.
RewriteRule ^component/users/\?view=registration$ registration.html$ [R=301,L]
You need a \ because ? is part of a regular expression. To Use it as a string u need to escape it with a \
Linktip: http://ole.michelsen.dk/tools/regex.html

.htaccess redirect for dynamic search pages

I need help with dynamic search pages. I need to change:
/download.php?q=search
to
/download.php?mp3=search
So only search pages need to change q to mp3.
I would appreciate if someone could help. I want to do this through .htaccess
Thanks in advance
The query string is only available as a variable with RewriteCond. Something like this should work:
RewriteCond %{QUERY_STRING} q=([^&]*)
RewriteRule ^download.php download.php?mp3=%1 [L,QSA,NC]
something like that:
RewriteRule ^/download.php?q=(.*) /download.php?mp3=$1 [L]

mod_rewrite remove query string on top of a rewrite rule

I have the following rewrite rule:
RewriteRule ^(.*)-task-(.*)\.html$ /index.php/task/name/$2\-task\-$1 [L]
When I tried to open:
/heru-task-number-1.html
It is working fine. HOwever, when there is a query string appended to it:
/heru-task-number-1.html?whatever=value
It is actually not calling the correct rewrite. Thus, I wonder how can I make sure so that both:
/heru-task-number-1.html
AND
/heru-task-number-1.html?whatever=value
are actually calling the same thing that is:
/index.php/task/name/$2\-task\-$1
I have tried to do this but to no avail.
RewriteRule ^(.*)-task-(.*)\.html\?(.*)$ /index.php/task/name/$2\-task\-$1 [L]
Thank you for your help or feedback on this.
This is fixed by inserting the following code at the top of htaccess:
RewriteCond %{QUERY_STRING} (^|&)fb_comment_id=
RewriteRule ^(.*)$ /$1? [L,R=301]
basically, what it does is that it will remove any extra query string that has fb_comment_id and redirect 301 to the one without query string.
Thank you #oddant and #Gerben for helping out!

How Can I ReWrite flat link to a dynamic link and preserve the Query string?

I am wanting to rewrite a url like:
http://my.project/mydomain.com/ANY_NUMBER_OF_CATEGORIES/designer/4/designer-name/page.html
to this:
http://my.projects/mydomain.com/ANY_NUMBER_OF_CATEGORIES/page.html?designer=4
I would like to use mod-rewrite to accomplish this.
Things to note:
Any number of categories can be between 'mydomain.com/' and '/designer'. For instance the url could be http://my.project/mydomain.com/designer/4/designer-name/page.html or it could be http://my.project/mydomain.com/tops/shirts/small/designer/4/designer-name/page.html
A query string may be provided in the original url that needs to be preserved in the rewritten url.
For example url provided could be: http://my.project/mydomain.com/designer/4/designer-name/page.html?color=red&type=shirt
Given the url above the resulting url would need to be: http://my.projects/mydomain.com/page.html?designer=4&color=red&type=shirt
The order of the query string does not matter. The 'designer=4' part could come before or after the rest of the query string.
I'm new to .htaccess and re-writes so any examples and or explanations would be greatly appreciated. Thank you very much.
Try this in your .htaccess file:
RewriteEngine on
RewriteRule ^(.+/)?(\d+)/[^/]+/([^/]+\.html)$ $1$3?designer=$2 [L,QSA]
I ended up having to create two separate RewriteRules:
RewriteRule ^designer/([^/]+)/.*/([^/]+)\.html $2.html?designer=$1 [NC,L,QSA]
RewriteRule (.*)/designer/([^/]+)/.*/([^/]+)\.html $1/$3.html?designer=$2 [NC,L,QSA]
Thanks to Gumbo for pointing out the [QSA] portion of the rewrite.

Resources