URL rewriting variable name - .htaccess

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

Related

How can I add conditional url variable using 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]

url rewriting and moving of questionmark

I've tried all manor of rules and just can work this one out. If there someone who can solve this?
I need to turn this
http://localhost/search/?terms=foobar
into this
http://localhost/index.php?m=search&terms=foobar
Where "foobar" is really ([^/]*), ie can be anything, it's a search string.
The initial URL has a "?" in it due to the GET method of HTML forms. Although it would be easier without that "?" it's not something that can be done?
I've tried the following with no luck.
#RewriteRule ^search/\?terms\=([^/]*)$ index.php?m=search&terms=$1 [L]
Thanks.
For query string you need to use %{QUERY_STRING}:
RewriteCond %{QUERY_STRING} ^terms=(.*)$ [NC]
RewriteRule ^search/?$ index.php?m=search&terms=%1 [NC,L]
You cannot match QUERY_STRING in RewriteRule. Have your code like this:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^(.+)$
RewriteRule ^(search)/?$ index.php?m=$1 [L,NC,QSA]
Because of QSA flag %{QUERY_STRING} will automatically be carried over to new URL.

.htaccess RewriteRule is not working

RewriteRule new/$ /search.php?category=1
RewriteRule new/\?(.+)$ /search.php?category=1&$1
I'm trying to do something like this, if the following address link is accessed,
http://onlineshop.com/new/
http://onlineshop.com/new/?price_max=30
then it will open this link,
http://onlineshop.com/new/search.php?category=1
http://onlineshop.com/new/search.php?category=1&price_max=30
Unfortunately it is not working this way.
A RewriteRule won't naturally catch query string parameters, you must use this kind of .htaccess :
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^new/$ /search.php?category=1&%1
You can just simply redirect from /new to /search.php with QSA flag and Apache will append the existing query string. Something like this will work for you:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^new/?$ /search.php?category=1 [L,QSA,NC]

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 not matching

htaccess not matching
RewriteCond %{REQUEST_URI} ^/catalog/products_in_scene.php?(.*)$
RewriteRule ^(.+) "/services/hpv/index.php?%1"
RewriteCond %{REQUEST_URI} ^/shop/derivation_tree.php?(.*)$
RewriteRule ^(.+) "/services/dt/index.php?%1"
The top one matches fine with all the GET variables, the second one matches and sends me to the right page but never sends it the GET variables; Why?
Remove the quote from your RewriteRule.
If it doesn't work, that mean there is something wrong in you php GET variable.
You'd do better to use the [QSA] flag:
RewriteRule ^/shop/derivation_tree.php /services/dt/index.php [QSA]
Change your code to this:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^catalog/products_in_scene\.php$ services/hpv/index.php [R,L,NC]
RewriteRule ^shop/derivation_tree\.php$ services/dt/index.php [R,L,NC]

Resources