Strip or rewrite query string from cleaned url with htaccess - .htaccess

What I'm looking to do is strip a query parameter from a dynamic URL that needs to also be cleaned using HTACCESS.
The url first starts as http://testdomain.com/post.php?id=3326.
The url is cleaned with a rewrite to http://testdomain.com/post/3326/.
Some links get "?ref=no_set" appended to the end
Url now needs to be redirected from http://testdomain.com/post/3326/?ref=no_set to http://testdomain.com/post/3326/
In some cases I need to add a new query parameter "?ref=no_set" to the url, but I'm wondering if it's possible to strip that query param and still be able to use $_GET in PHP to retrieve it even though it isn't being rewritten.
Any help would be appreciated!

RewriteEngine on
RewriteCond [conditional reg ex here]
RewriteRule [rewrite reg ex here] - [CO=ref:no_set:.testdomain.com]

Simple...
RewriteEngine on
RewriteCond %{QUERY_STRING} !^$
RewriteRule .* http://testdomain.com%{REQUEST_URI}? [R=301,L]
# ADD CODE TO HANDLE YOUR REWRITES AFTER THIS
Have a nice day! ;)

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.

restricting input of GET url on htaccess rewrite url

My original url is : www.site.com/report.cgi?d=2012-05
Requested URL: www.site.com/report-2012-05.cgi
My Htaccess Code:*
RewriteRule ^report([^/]*)\.cgi$ /report.php?d=$1 [L]
I want to restrict the request parameter to just XXXX-XX number format in GET url.
How can I do this ?
I didn't really understand your question, except you want to modify the URL format placing the parameter value in a different position.
The best way to do it is by capturing the query string like this:
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} d=(.*)
The value inside the round brackets is the parameter value (2012-05), which can be back referenced with %1. For example:
RewriteRule .* report-%1.cgi [L]
Will rewrite the URL with /report-2012-05.cgi
Hope this helps.
I think you need to remove .cgi from your rewrite rule
For www.site.com/report-xxxx-xx
RewriteRule ^report-([^/]*)$ /report.cgi?d=$1 [L]<br>
For www.site.com/xxxx-xx
RewriteEngine On
RewriteRule ^([^/]*)$ /report.cgi?d=$1 [L]

RewriteCond appending querystring

I am using .htaccess file to redirect my archived old aspx urls to my new php site. Now the requirement is as follows:
example.com/showad.aspx?adid=1234 should be example.com/ad/getad/1234
I googled the matter and got the following htaccess rule:
RewriteCond %{QUERY_STRING} ^adid=(\d+)$
RewriteRule ^showad\.aspx$ /ad/getad/%1
The rule works except that the resulting url is example.com/ad/getad/1234?adid=1234
I searched again and learned that to remove the (adid=1234) at the end of the output url you should place a question mark ? at the end of the substitution parameter, so that the rule should be:
RewriteCond %{QUERY_STRING} ^adid=(\d+)$
RewriteRule ^showad\.aspx$ /ad/getad/%1?
But it doesn't work!
The resulting url is STILL having the original query string appended to it. I am using this site to test my htaccess rules: htaccess.madewithlove.be
Any clues why is this happening?
Try changing your rule to
RewriteRule ^showad\.aspx$ /ad/getad/%1? [R=302,L]
Your rule is fine - I just tested it in my own .htaccess. It's the htaccess tester website that's wrong :)

htaccess: Mediafire.com like urls

I'm trying to come up with some mod_rewrite to translate http://example.com/?7gudznrxdnu into http://example.com/view.php?id=7gudznrxdnu
But any other page will function properly such as http://example.com/contact and so on.
I think this will work:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^[a-z0-9]+$
RewriteRule ^$ view.php?id=%{QUERY_STRING} [L]
If you want the rewrite to be shown in the browser's address field, you'll have to replace [L] with [L,R=301].
Explanation: The query-string (what's following the question mark) is not part of the URL that RewriteRule sees in its matching-pattern, therefore you can't check for question mark there. In my solution, I run the rule if and only if (RewriteCond) the query string consists solely of a-z and/or 0-9, and my rule only rewrites URLs ending with a slash (except for the query string). I redirect this to view.php?id=, and then append the query string to that.
Edit: Tested on my Apache-server, and I haven't found any bugs (yet).
You should try (in your .htaccess):
RewriteEngine On
RewriteRule ^\?([^/\.]+)?$ view.php?id=$1 [L]

Regular Expression For URL Query

I've been using mod_rewrite in my htaccess file to rewrite my urls and I've run into a problem when doing pagination.
Here's the url for my page:
http://domain.com/concerts/features/folk-roots?page=2
htaccess file:
RewriteRule ^features/([^/]*)?page=([0-9]*)$ featureCat.php?cat=$1&page=$2 [NC,L]
It works fine without the page query, but if I want to change pages I can't figure out how to write the regular expression to grab the page #.
Any ideas would be appreciated!
You need to use RewriteCond immediately before the RewriteRule to get access to the query string:
RewriteCond %{QUERY_STRING} ^page=([0-9]*)$
RewriteRule ^features/([^/]*)$ featureCat.php?cat=$1&page=%1 [NC,L]

Resources