How can I add conditional url variable using htaccess? - .htaccess

I use following code:
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{QUERY_STRING} (^|&)post_type=product(&|$)
RewriteCond %{QUERY_STRING} !(^|&)product_cat=xyz
RewriteRule ^(.*)$ http://www.example.com/?%{QUERY_STRING}&product_cat=xyz
but it doesn't works.
I need to add variable "product_cat=xyz" where query_string contains "post_type=product" and it doesn't just contains "product_cat=xyz".
Examples:
If url is "http://www.example.com/?post_type=product&product_cat=xyz" then htacces leaves it unchanged.
If url is "http://www.example.com/?post_type=product" then htacces must add "&product_cat=xyz"
:-) Can you help me, please?
Thanks in advance.

You can use:
RewriteEngine on
RewriteCond %{QUERY_STRING} (^|&)post_type=product(&|$)
RewriteCond %{QUERY_STRING} !(^|&)product_cat=xyz(&|$)
RewriteRule ^ http://www.example.com/?product_cat=xyz [QSA,L]

Related

change site path by RewriteRule3

i need change the site url path
from
site.net/open.php?cat=22&book=2285
to
site.net/book/2285
i think this code need edit and complite
RewriteEngine On
RewriteRule ^book/([^/]*)$ /books/open.php?cat=22&book=$1 [L]
how to do it by .htaccess and RewriteRule?
i need code work in all categorys also not 22 only
This code should work for you -
RewriteEngine on
RewriteCond %{QUERY_STRING} (^|&)cat\=22($|&)
RewriteCond %{QUERY_STRING} (^|&)book\=2285($|&)
RewriteRule ^open\.php$ /book/2285? [L,R=301]

change the url using with htaccess

i am trying to change the url using htaccess rewrite
i want to change this url
page/details.php?name=abcdef&id=18
to
page/abcdef
Here my sample code for this
RewriteEngine On
RewriteRule ^company/([A-Za-z0-9-]+)/$1 company/details.php?name=$1&id=$2 [R=301,L]
this is not working, and also i was tried many code but not working,please help me to find htaccess code for this url
Thanks advance
This should do what you're after:
RewriteCond %{QUERY_STRING} ^.*name=([a-zA-Z0-9]*).*$
RewriteRule ^page/(.*)$ page/%1? [R=301,L]
Try this one:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^name=(.*)&id=(.*)$
RewriteRule ^page/(.*)$ /page/%1? [R=301,L]
This will check for these 2 parameters in the query string.

htacess redirect if url stops with specific word

Is it possible to redirect based on an url that would be as:
www.url.com/stuff.php
But not redirect if the url contained any characters after the php, as such:
www.url.com/stuff.php?variables=values&othervariable=othervalue&etc=more
I have no clue how to wriite this either.
Thanks in advance!
Edit: basically I need the redirect to happen to www.url2.com only if www.url.com ends with .php, but not if the .php is followed by variables.
try
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^/site.com/?$ /site.com/cart.php [R=301,NC,L]
Try:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^/?cart.php$ / [L,R=301]
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^stuff.php$ RELATIVE_OR_ABSOLUTE_URL_HERE [L]

htaccess - redirect query string and remove it from URL

I search many topic about htaccess but still not success.
I want when people type address:
http://domain.com/?q=filename1
http://domain.com/?q=filename2
...
It will auto redirect to:
http://domain.com/download/filename1.html
http://domain.com/download/filename2.html
...
I try:
RewriteEngine On
RewriteRule ^/?q=(.*)$ /download/$1.html [L,R=301]
But it is not working. How can I fix this?
Using this may be helpful:
RewriteEngine On
RewriteCond %{QUERY_STRING} q=(.*)
RewriteRule ^q(.*) /download/%1.html [L,R=301]
EDIT:
Try this :)
RewriteEngine On
RewriteCond %{QUERY_STRING} q=(.*)
RewriteRule ^(.*) /download/%1.html? [L,R=301]
By using ? query string will be removed ;)

URL rewriting variable name

I am trying to rewrite an URL so that a passed variable is replaced with a new id, for example;
wwww.domain.com/default.php?s=1&lang=en
To be rewritten to:
www.domain.com/default.php?id=1$lang=en
The s variable being replaced with id
I have tried:
RewriteEngine on
Options +FollowSymlinks
RewriteBase /
RewriteCond %{QUERY_STRING} ^s=(.+)$
RewriteRule ^s=(.+)$ id=$1 [R,NC,L]
also
RewriteRule ^s=(.+)$ id=%1 [R,NC,L]
no luck... what am I doing wrong?
Thanks for any help!!
This should work if even if you don't have s as the first variable. (Not sure what you really want.)
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{QUERY_STRING} ^(.*&)*s=(.*)(&*)$
RewriteRule ^default.php$ default.php?%1&id=%2%3
Try this out:
RewriteRule ^default.php?s=([0-9]+)&lang=en$ default.php?id=$1&lang=en
Edit.
A more generic version.
RewriteRule ([^?]+)?s=([0-9]+)&lang=([A-z]{2})$ $1?id=$2&lang=$3

Resources