how to add a variable to an existing url using htaccess - .htaccess

I want to rewrite a url from
https://www.example.com
to
https://www.exemple.com?sourcecode
or
https://www.example.com/page1?sourcecode
by adding a variable to the end.

That might be due to other rules in your .htaccess. Use this rule to capture original string from THE_REQUEST variable. And add ? in the end to strip off query string. try this:
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/+example(\S*)\s [NC]
RewriteRule ^ http://example.com/page1/%1? [R=301,L,NE]

Related

htaccess redirect whilst parsing query string and variable

How do you redirect a subdomain whilst capturing anything after the '?' and appending to a redirect using htaccess, whilst maintaining a particular variable?
redirect from...
http://subdomain1.example.com/?retailcentre=subdomain1&utm_source=twitter&utm_medium=twitter&utm_campaign=022641example
to...
http://www.example.com/?retailcentre=subdomain1&utm_source=twitter&utm_medium=twitter&utm_campaign=022641example
The query string after the '?' could be anything as it could come from twitter, facebook etc.
The one var that has to be parsed is what subdomain the redirect came from, in this case 'retailcentre'.
Try with:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^subdomain1\.example\.com$ [NC]
RewriteRule ^ http://www.example.com%{REQUEST_URI}?retailcentre=subdomain1 [QSA,NE,R=301,L]
[QSA] Flag: When the replacement URI contains a query string, the default behavior of RewriteRule is to discard the existing query string, and replace it with the newly generated one. Using the [QSA] flag causes the query strings to be combined.
You can use the following rules :
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.example\.com$
RewriteCond %{THE_REQUEST} /\?retailcentre=.+&utm_source=.+&utm_medium=.+&utm_campaign=.+\sHTTP [NC]
RewriteRule ^ http://example.com/ [L,R,NE]

How to remove an element/parameter from URL by .htaccess

I would remove color_new=any-value-could-be-here anywhere in URL via .htaccess
So how to do it?
You can use this rule to remove a query parameter from any where in query string:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^(.+?&)?color_new=[^&]*(?:&(.*))?$
RewriteRule ^ %{REQUEST_URI}?%1%2 [R=302,L]
Keep this rule above other rules.

.htaccess rewriterule for folder

I'm trying to redirect using the link: http://www.mysite.com/af/2 to http://www.mysite.com/af/2/, but I can not perform this procedure, the link /af/#### corresponds to a user ID and is a folder.
You can accomplish this using .htaccess?
A rule like the following will take care of that:
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^ %{REQUEST_URI}/ [R=301,L]
The condition is to prevent urls that already end with a slash to match this rule. The rule will match everything ^ and redirect it to the same url with a slash behind it.

URL Masking with htaccess

I have a url below:
http://www.mysite.com/mypage.php?PropertyBuyRent=rent&Developer=&Resort=&City=&State=&Country=&Price=
if PropertyBuyRent=rent and all other variables are empty then url should be like:
http://www.mysite.com/mypage.php/TimeshareForRent/All
how can i write htaccess for this?
You can use this rule:
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(testing/mypage\.php)\?PropertyBuyRent=rent&Developer=&Resort=&City=&State=&Country=&Price= [NC]
RewriteRule ^ /%1/TimeshareForRent/All? [R=301,L]
RewriteRule ^(testing/mypage.php)/TimeshareForRent/All/?$ /$1?PropertyBuyRent=rent&Developer=&Resort=&City=&State=&Country=&Price= [L,NC,QSA]
Reference: Apache mod_rewrite Introduction
You can use this line and make edits in php file as follows
RewriteRule ^mypage.php/TimeshareForRent/All/?$ mypage.php?PropertyBuyRent=rent&Developer=&Resort=&City=&State=&Country=&Price= [L,NC,QSA]
You can tell your php file to redirect to "mypage.php/TimeshareForRent/All" when your condition matched, because with above rule, it will mask the url of variables with the one without variable

Remove variable from base URL with htaccess

I've been trying to rewrite a URL such as
www.somesite.com/?x=372
into a url
www.somesite.com/
My current code does not seem to work
RewriteEngine On
RewriteCond %{QUERY_STRING} x=(.*)
RewriteRule http://www.somesite.com/ [R=301,L]
I've looked up countless ways of trying to do it with htaccess and still no success.
If you simply want to redirect a client to remove the query string (everything after the ? in the URL), then you can try this:
RewriteEngine On
RewriteCond %{QUERY_STRING} x=(.*)
RewriteRule ^ http://www.somesite.com/? [R=301,L]
You've got most of it right, it seems, but your rule needs to have a match, and your target (the http://www.somesite.com/) needs a ? at the end so that any query string before the rewrite won't get appended.
In Apache 2.4 or newer you can use the QSD query string discard flag:
RewriteEngine On
RewriteCond %{QUERY_STRING} x=(.*)
RewriteRule .* http://www.somesite.com/ [R=301,L,QSD]

Resources