htaccess Rewrite Rule query string to url - .htaccess

I have following url:
www.example.com/profile?id=31
And I want to redirect it to :
www.example.com/profile/id/31
How can I do this ??
I tried using this:
RewriteCond %{QUERY_STRING} (^|&)id=31($|&)
RewriteRule ^www\.example\.com/profile$ /www.example.com/profile/id/31?&%{QUERY_STRING}
But it is not working

Try with below,
RewriteCond %{QUERY_STRING} ^(.+)=(.+)
RewriteRule ^(.+)$ http://www.example.com/$1/$2/$3 [R=301,L,QSD]
I am using match groups so it will work for any value for given similar pattern.

I will go for something like this let me know if it helps:
RewriteRule ^ profile/([0-9]+)/?$ /profile?id=$1 [NC,L,QSA]
what it does is explained widely in https://stackoverflow.com/a/20566547/6517383 and other answers in this post.

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]

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

remove second question mark from query string

I made a mistake in creating links. I corrected it but now still there are links floating around that might look like this:
http://www.domain.com/?page=1?date=29062015&id=778
I would like to correct this using the rewriteEngine to redirect my users to:
http://www.domain.com/?page=1&date=29062015&id=778
I searched around and tried the following, but it doesn't work:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^(.*)\?(.*)$
RewriteRule ^\/ ^\/$1\&$2 [L,R=301]
What should I change here?
I've slightly corrected your rule,hope it will help to resolve the issue:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^(.*)\?(.*)$
RewriteRule (.*) $1?%1&%2 [L,R=301]
Note that %1 and %2 are back-references to the matched part of the regular expression in the previous RewriteCond (Query string condition).
Good article about rewriting query string here: https://wiki.apache.org/httpd/RewriteQueryString

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

Resources