Htaccess redirect issue with query string - .htaccess

I have URL domain.com/index.php
I have written this code to redirect to clientarea.php
RewriteEngine On
RewriteRule ^index.php /clientarea.php [R=301]
Now this works except for urls that contain a query string (eg. domain.com/index.php?=something). This will also redirect but I don't want it when there is a query string.
Can anyone tell me how I can do it ?

To prevent rewriting when a query string is there
RewriteEngine On
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^index\.php$ /clientarea.php [R=301,L]

RewriteEngine On
RewriteRule ^index.php$ /clientarea.php? [R=301]
$ marks the end of the string in the regex.

Related

Apache redirect to main URL on Query

i have requirement to redirect Apache on query base parameters for example
https://example.com/?ampostpreserve=01902018
I need to redirect to https://example.com .. I tried with
RewriteEngine On RewriteCond %{QUERY_STRING} ^ampostpreserv$
RewriteRule (.*) https://example.com [R=301,L]
but seem not working ..any solution
Thanks Hem
It seems like you just want to drop the query string? The QSD parameter does exactly that. The below version is for every called URL.
RewriteEngine On
RewriteCond %{QUERY_STRING} .+
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [QSD,L,R=301]
And this is the specific version for example.com and the above query string:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^ampostpreserve
RewriteRule .* https://example.com [QSD,L,R=301]

Add query string to redirect URL in .htaccess

How can I add s string to the end of a search result using .htaccess?
Original link: www.example.com/?s=search1 redirect to:
www.example.com/?s=search1&post_type=product
or
www.example.com/?s=search2 redirect to:
www.example.com/?s=search2&post_type=product
"search1" and "search2" are variable.
I need this to change a Wordpress search result to a Woocommerce search result.
What a tried:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^s=([^&]+)
RewriteRule ^/?s=$ http://www.example.com/?s=%1&post_type=product [L,R=301]
Thank you in advance.
Your .htaccess seems fine, except for one thing: the RewriteRule Pattern will be matched against the part of the URL after the hostname and port, and before the query string, so this should do:
RewriteEngine On
RewriteCond %{QUERY_STRING} !post_type=product
RewriteCond %{QUERY_STRING} ^s=([^&]+)
RewriteRule ^$ http://www.example.com/?s=%1&post_type=product [L,R=301]

Put a parameter after url suffix

Using htaccess or any other way how can I pass a parameter to end of url suffix
EX: I need all the .html to be html?v=1
I tried the following
RewriteEngine on
RewriteBase /
RewriteRule ^(.*)\.html$ $1.html?v=1 [R]
But Its not working
Check first that the query string does not already exist:
RewriteEngine On
RewriteCond %{QUERY_STRING} !v=1
RewriteRule ^(.*)\.html$ $1.html?v=1 [R,L]

Rewrite rule based on query string

I'm trying to redirect to the base url (www.example.com) requests that have a particular query string (when option is com_estateagent).
I've tried the following syntax:
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^option=com_estateagent$
RewriteRule .* index.php [R=301, L]
But it gets ignored.
Any suggestions?
EDIT
The url that I want to change is something like this:
http://www.example.com/subdirectory/index.php?option=com_estateagent...
I don't think you need the RewriteCond directive, wouldn't something like this work?
RewriteRule ^/.*option=com_estateagent /index.php[R=301,L]
This works
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} option=com_estateagent
RewriteRule .* subdirectory/index.php? [R=301, L]
Notice, the ? after index.php. That's important if you want to drop Query Parameters on redirect.

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 ;)

Resources