Replace part of uri with .htaccess - .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]

Related

How to rewrite query string to hash in the URL?

I am looking to rewrite:
www.mysite.com/services/category1/?content=service1
to
www.mysite.com/services/category1/#tab-service1
I have this specific rewrite that I want to generalize for all the possible URLs on the site:
RewriteCond %{QUERY_STRING} content=service1
RewriteRule ^services/category1 services/category1/#tab-service1? [NE,R,L]
I tried the following but it didn't work :( -
RewriteCond %{QUERY_STRING} content=([a-zA-Z]+)
RewriteRule ^services/category1/?content=(.*)$ services/category1/#tab-$1 [NE,R,L]
Thanks.
You rule looks fine except for 1 thing
QUERYSTRING content=service1
isn't part of match in RewriteRule's pattern so you can't test query string (Url part after the ?) in pattern of a RewriteRule. To test url query string we use %{QUERY_STRING} variable like the one you are already using.
RewriteCond %{QUERY_STRING} content=([a-zA-Z]+)
RewriteRule ^services/category1/?$ services/category1/#tab-%1 [NE,R,L]

URL rewrite for a clean format

i am trying to have my urls look like cleaner using the below but its not working i don't know if i should add anything else,
this is how it looks now
example.com/product.php?product_id=144&product_name=50ml-bottle-with-glasses,-shirt
this is how i want them to look
example.com/product/144/50ml-bottle-with-glasses,-shirt
basically here's what i used
RewriteEngine On
RewriteRule ^product/([0-9]+)/([A-Za-z0-9-]+)/?$ product.php?product_id=$1&product_name=$2 [NC,L] # Handle product requests
Thank you
This should work :
RewriteEngine On
#1)redirect from "/product\.php\?product_id=123&product_name=foo" to "/product/123/foo"
RewriteCond %{THE_REQUEST} /product\.php\?product_id=([^&]+)&product_name=([^\s&]+) [NC]
RewriteRule ^ /product/%1/%2? [NE,L,R]
#2)internally map "/product/123/foo" to "/product\.php\?product_id=123&product_name=foo"
RewriteRule ^product/([0-9]+)/(.+)/?$ product.php?product_id=$1&product_name=$2 [B,NC,L]
There are comma into your parameter product_name. Try to change your .htaccess and add comma into your regular expression for this parameter: ([A-Za-z0-9-,]+). So your htaccess will be something like this:
RewriteEngine On
RewriteRule ^product/([0-9]+)/([A-Za-z0-9-,]+)$ product.php?product_id=$1&product_name=$2 [NC,L] # Handle product requests

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

htaccess redirection to new params

i want an html redirection to a different php and params, the phps are inside a subdirectory , for example, from an /subdir/index.php?thread=23-post=12 /subdir/to showthread.php?topic=23&topic=12. But isn't working this:
RewriteRule ^/test/index\.php?thread=(.*)-post=(.*)$ /test/showthread.php?topic=$1&topic=$2 [R]
Any suggestion?
thanks in advance
You can't match against the query string in a RewriteRule. You need to match against the %{QUERY_STRING} var in a RewriteCond and use the % backreference:
RewriteCond %{QUERY_STRING} ^thread=([^-]+)-post=(.*)$
RewriteRule ^/?test/index\.php$ /test/showthread.php?topic=%1&topic=%2 [L,R]
Note that your rule maps 2 things to the topic query string param.

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]

Resources