restricting input of GET url on htaccess rewrite url - .htaccess

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]

Related

Rewrite url with .htaccess based on GET parameters

I want to rewrite the following url pattern:
http://example.com/some/inner/page?param1=test&lang=en
becomes
http://example.com/en/some/inner/page?param1=test
. So basically I want to take the "lang" get parameter and put it at the begginning of url. I know I have to use RewriteRule in the .htaccess but what is the pattern that has to be written ? Note that I also need it to work for both ?lang=en and ?some-parameter=test&lang=en
You can use this rule as your top most rule in root .htaccess:
RewriteEngine On
RewriteCond %{THE_REQUEST} \?(.*&)?lang=([^&]+)&?(\S*)\sHTTP [NC]
RewriteRule ^ /%2%{REQUEST_URI}?%1%3 [R=301,NE,L]
This will remove lang= parameter from anywhere in query string and redirect using value of same parameter at the front.

How to rewrite a long URL with numerical value to short url with same value

Please Help with non-stardart Long URL
http://example.com/media/player/player.swf?f=http://example.com/media/player/config.php?vkey=12345
Want rewrite to short URL
http://example.com/embed/12345
I assume you mean it the other way around, if someone request http://example.com/embed/12345 you want this internal rewritten to http://example.com/media/player/player.swf?f=http://example.com/media/player/config.php?vkey=12345
If so this works as follows:
RewriteEngine On
RewriteRule ^/?embed/([^/]+)$ /media/player/player.swf?f=http://example.com/media/player/config.php?vkey=$1 [L]
If you really want it the other way around than you have to do it like so:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^f=http://example\.com/media/player/config\.php\?vkey=([^&]+)$
RewriteRule ^/?media/player/player\.swf$ /embed/%1 [L]
If you want a redirect than change [L] to [R=301,L]

How to make a htacess RewriteRule with a url variable

I want to redirect the url http://www.mywebsite.com/tutos/tutos.php?q=my-tuto/tuto-1.html to
http://www.mywebsite.com/tutos/tutos/my-tuto/tuto-1.html, how to do that with a .htaccess ?
I tried this, but it's don't working... :
RewriteRule ^http://www.mywebsite.com/tutos/([^-]*)$ http://www.mywebsite.com/tutos/tutos.php?q=$1 [L,QSA]
Thanks !
The rule that you have seems to be doing the opposite of what you say that you want. It's redirecting the non-query-string URL to the query-string URL. But if that's what you want, you need to remove the host and protocol from the regular expression. Only the URI (sans query string) is used to match against in a RewriteRule:
RewriteRule ^tutos/([^-]*)$ /tutos/tutos.php?q=$1 [L,QSA,R=301]
But if you wanted it the other way around like you had asked:
RewriteCond %{QUERY_STRING} (.*)(^|&)q=([^&]+)(.*)
RewriteRule ^tutos/tutos.php$ /tutos/%2?%1%3 [L,R=301]

htaccess rewrite querystring and remove empty value

first, sorry for my bad English.
I try to rewrite url generated from Form Get and redirect that.
my url is like this:
http://www.mysite.com/properties?action=search&agreement=for-rent&category=my-category&type=&zone=my-zone&city=my-city
and I have this .htaccess configured:
11. RewriteCond %{QUERY_STRING} ^action=(?:[a-zA-Z\-]*)&(?:.*)=([a-zA-Z\-]*)&(?:.*)=([a-zA-Z\-]*)&(?:.*)=([a-zA-Z\-]*)&(?:.*)=([a-zA-Z\-]*)&(?:.*)=([a-zA-Z\-]*)$
12. RewriteRule (.*) %{REQUEST_URI}/%1/%2/%3/%4/%5/? [R=301,L]
So basically all my request are direct to index.php.
21. RewriteCond %{REQUEST_URI} !index\.php|resources|hidden
22. RewriteRule ^(.*)$ index.php/$1 [L]
All works, but the problem is when I have an empty value in query string, the rule add double slash and the above url (for example whit &type=&zone=my-zone... type have empty value) will translate like that:
http://www.mysite.com/for-rent/my-category//my-zone/my-city/
The question is: How can i remove in .htaccess the double slash generated if i have one or more empty value in query string?
Thanks
Easiest is to do another redirect (not real pretty as it requires two 301's).
RewriteCond %{THE_REQUEST} //
RewriteRule .* $0 [R=301,L]
The fun part is that when the url is loaded with a double slash in it, mod_rewrite will automatically remove this. So as you can see above you'll just have to rewrite the url to itself, kind of.

.htaccess redirect from GET variable to url string

I need to redirect
/search?keywords=somesearchterm
to
/search/somesearchterm
This seems incredibly basic but I've been pounding my head against it for an hour.
Thanks for taking the time to look at this.
You want to implement what is called a "301 Redirect" with mod_rewrite.
RewriteEngine ON
RewriteRule ^/search\?keywords=somesearchterm$ /search/somesearchterm
adding regular expressions:
RewriteEngine ON
RewriteRule ^/search\?keywords=(.+) /search/$1 [R=301,L]
R=301 means provide a 301 Header redirect so the user's URL changes in the browser, and L means don't process any more rewrite rules if this one matches.
If you want to do the reverse -- in other words, if someone goes to mysite.com/search/asearchterm and you want the URL to stay the same, but "behind the scenes" you want it to load a certain server script, do this:
RewriteEngine ON
RewriteRule ^/search/(.+) /search.php\?keywords=$1 [L]
You can not match aginst Query string in RewriteRule directive. Use %{THE_REQUEST} or %{QUERY_STRING} server variables to match the Query string :
The following rule works fine for this redirection
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/search\?kewords=([^&\s]+) [NC]
RewriteRule ^ /search/%1? [NE,NC,R,L]
RewriteRule ^search/([^/]+)/?$ /search?keyword=$1 [QSA,NC,L]

Resources