How to make a htacess RewriteRule with a url variable - .htaccess

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]

Related

Replace part of uri with .htaccess

I want to replace get parameter with mod_rewrite in .htaccess. I have Url www.domain.at/success?id=12345 and need to replace "id" with "vid" -> www.domain.at/success?vid=12345
This replacement must only work on "success" page/uri, but not on other pages of website.
I tried
RewriteEngine On
RewriteRule ^(.*)success?id=([^0-9]*)$ /$1success?vid=$2 [R=301,L]
But this is not working on dynamic part?
Thanks for help!
Martin
You have to match query parameters in RewriteCond separately from request URI like this:
RewriteCond %{QUERY_STRING} ^id=(.+)$ [NC]
RewriteRule ^success/?$ /$0?vid=%1 [R=301,L,NC]

htaccess redirect specific url to another domain

I want to redirect specific URL with params to another domain
http://domain.tld/buy/?w=X1234 to http://anotherdomain.tld/product.php?id=X1234
Here my htaccess
RewriteCond %{REQUEST_URI} !^/*buy/(.*)$ [NC]
RewriteRule ^/*buy/?w=([a-zA-Z0-9]+)$ http://anotherdomain.tld/product.php?id=$1 [NC,L,R=301]
but not working. maybe the problem is param ?w=
Thank you
UPDATE: i solve the problem by using Query string, after reading How to REGEX and .htaccess rewrite url
RewriteCond %{QUERY_STRING} ^w=([a-zA-Z0-9]+)$
RewriteRule ^/*buy/$ http://anotherdomain.tld/product.php?id=%1 [NC,L,R=301]
Thank you stackoverflow and folks! i m start understanding regex from here ^_^
That's right, your params won't be picked up, but they can be passed on.
Something like;
RewriteRule ^buy/([a-zA-Z0-9]+)$ http://anotherdomain.tld/product.php?id=$1 [NC,L,R=301]
Where your original url would be; /buy/X1234

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]

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