How to do a htaccess rewrite with a question mark ? in the original url - apache

I've got to redirect a URL that has a question mark in it, and I just can't figure out the proper .htaccess code to make it happen.
Example:
redirect 301 /home.php?cat=16 http://www.example.com/furniture.html
I believe I need to do a rewrite condition, but I'm no htaccess expert.

With the Redirect directive from mod_alias you can only examine the URI path and not the query (applies to all directives of mod_alias). If you want to examine the URI query you need to use mod_rewrite:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^cat=16$
RewriteRule ^home\.php$ http://www.example.com/furniture.html? [L,R=301]
The empty query in the replacement will prevent that the original query is being appended to the new URI.

From the apache2 mod_rewrite docs:
What is matched?
The Pattern will initially be matched
against the part of the URL after the
hostname and port, and before the
query string. If you wish to match
against the hostname, port, or query
string, use a RewriteCond with the
%{HTTP_HOST}, %{SERVER_PORT}, or
%{QUERY_STRING} variables
respectively.

for more complex parameters (like site.com/index.php?blaa=1&id=33 )
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{QUERY_STRING} blaa=1&id=33
RewriteRule ^(.*)$ http://www.site.com/yourpage.html? [R=301,L]
</IfModule>

Related

htaccess rewrite rule from folder to folder with querystring

I am looking to include a rewrite rule for the following but can't seem to get it to work. I don't want to pass any query string in but I need to add one to the rule.
I want this URL:
https://example.co.uk/vehicles/
to point to:
https://example.co.uk/search-results/?category=1
but keep the first URL in the address bar.
I need to pass in a variable called category with a value.
I tried the following but it didn't work for me:
rewriterule ^vehicles/$ search-results/?category=1 [NC, L]
Any help would be appreciated.
RewriteEngine On
RewriteRule .* ? [F,L]
RewriteRule ^(.*)$ https://example.co.uk/$1 [R,L]
RewriteRule ^ad/(.*/)?([0-9]+)$ view-ad/?ad=$2 [NC,L]
RewriteRule ^vehicles/$ search-results/?category=1 [NC,L]
I managed to solve it. It was due to an Ajax load on the page.
Glad you solved your initial query, however, the following two directives in your posted .htaccess file will break your site, so presumably, these have already been removed?
RewriteRule .* ? [F,L]
RewriteRule ^(.*)$ https://example.co.uk/$1 [R,L]
The first directive simply blocks all access to your site, returning a 403 Forbidden response. And the second directive will result in a redirect loop.
Now that it works is it possible to have another rewrite rule that does this https://example.co.uk/vehicles/?something=1 and rewrite to https://example.co.uk/vehicles/?category=1&appendsomething=1 but only display https://example.co.uk/vehicles/
I assume you mean https://example.co.uk/search-results/?category=1&something=1 (as opposed to /vehicles/) - where something=1 is appended on the end of the query string?
You wouldn't be able to make this "display as https://example.co.uk/vehicles/" - as this would conflict with your existing (working) directive.
However, you could potentially modify your existing directive to handle requests for /?something=1 and pass this through to the substitution. This would simply require the addition of the QSA flag (Query String Append). For example:
RewriteRule ^vehicles/$ search-results/?category=1 [QSA,NC,L]
The QSA flag results in the query string from the request being appended to the end of the query string specified in the RewriteRule substitution.
UPDATE: To redirect HTTP to HTTPS, you would need something like the following instead:
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://example.co.uk/$1 [R,L]
Note the preceding RewriteCond directive - this ensures that only HTTP requests are redirected, not everything (HTTP and HTTPS), so avoiding a redirect loop. Ultimately this should also be a 301 (permanent) redirect, so you should change R to R=301, but only when you are sure it's working OK.

301 Rewrite Rule Not working

We want to redirect Below mentioned URL.
From URL:
www.example.com/itemLevelFilterPage.action?keyWordTxt=&srchTyp=CATNAV&attrFilterList=attr_brand%3A%223M%22&resultPage=0
To URL : www.example.com/4G
We wrote 301 rules in apache configuration as below.
RewriteEngine on
RewriteRule ^/itemLevelFilterPage.action?keyWordTxt=&srchTyp=CATNAV&attrFilterList=attr_brand%3A%223M%22&resultPage=0 /4G [L,R=301]
But redirection is not working as expected. Any Suggestion will be highly appreciated.
mod_rewrite will not look at query strings as part of its matching, unless you specifically ask it to:
RewriteCond %{REQUEST_URI} ^/itemLevelFilterPage\.action$
RewriteCond %{QUERY_STRING} ^keyWordTxt=&srchTyp=CATNAV&attrFilterList=attr_brand%3A%223M%22&resultPage=0$
RewriteRule (.*) http://www.example.com/4G? [R=301,L]
The first condition makes sure we're looking at the right page, the second checks the query string, and the rule will rewrite the URL to the desired one.
The trailing question mark on the rule ensures the old query string is removed. If you're on Apache 2.4 the query string discard flag is available:
RewriteRule (.*) http://www.example.com/4G [R=301,L,QSD]
More examples here.

Rewrite part of url with .htaccess

I need to rewrite URL and save top level domain and query.
I tried to use these rules
RewriteEngine On
RewriteBase /
RewriteRule ^domain(.*)$ http://newdomain$1 [R=302,NE,L]
Using this testing tool I found that it works if domain.com/query?param=value is used as a request URL. But if I try to use http://domain.com/query?param=value[ it doesn't work.
Basically I don't care what protocol is (http or https), I just need to replace first occurrence of domain string and rewrite it with newdomain saving all other parts of a request URL.
As it turned out in your comment to the first answer I gave you are actually trying to replace only part of the hostname of the incoming request but keep path and query string. That was not clear to me from your question, sorry.
You have to use an additional RewriteCond for this, since as said before you caanot access the hostname at all inside a RewriteRule. So I guess the following goes into the direction of what you are actually looking for:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^[^\.]+\.(.+)$
RewriteCond %{HTTP_HOST} !^newdomain\.(.+)$
RewriteRule ^(.*)$ http://newdomain.%1/$1 [R=301,L,QSA]
You may want to try this modification to preserve the original request scheme too:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^[^\.]+\.(.+)$
RewriteCond %{HTTP_HOST} !^newdomain\.(.+)$
RewriteRule ^(.*)$ %{REQUEST_SCHEME}://newdomain.%1/$1 [R=301,L,QSA]

Apache rewrite - match query string

I understand that using rewrites in apache if i want to match the query string then i should use
RewriteCond %{QUERY_STRING}
Which is fine, however, I have a csv with around 2000 urls to rewrite, a lot contain random query strings. It will be a painstaking process to go through each and create the rule.
Is there any generic way to have the rewrite look at the entire url, including the query string and redirect it?
try rewritemap
example config for vurtual host
RewriteEngine on
RewriteMap mymap "txt:/path/to/map.txt"
RewriteCond %{QUERY_STRING} ^(.+)$
RewriteCond ${mymap:%1} >""
RewriteRule ^(.*)$ ${mymap:%1} [L]
and map.txt example
p=1&i=1 /test.php?n=2
p=1&i=2 /test.php?n=4
p=1&i=3 /test.php?n=6
...
Suppose that it is suitable for site with no hign load :)

removing directory in apache mod_rewrite

I have a PHP site which replaces an ASP site, so the path structure is different.
In the URLs, I need to match http://apache.site/Cartv3/Details.asp & redirect to another location. What is the correct syntax to match that URL fragment?
I've already tried
RewriteCond %{REQUEST_URI} CartV3/results1.asp?Category=60
RewriteRule ^(.*)$ home-study/A-Levels/1/page-1 [R=301,L]
and
RewriteRule ^CartV3/Details\.asp?ProductID=1004 home-study/A-Levels/1/page-1 [R=301,L]
You meed to read more about mod_rewrite. Remember RewriteRule doesn't match query string. You attempt needs to be rewritten as:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^Category=60$ [NC]
RewriteRule ^CartV3/results1\.asp$ /home-study/A-Levels/1/page-1? [R=302,L,NC]
Once you verify it is working fine, replace R=302 to R=301. Avoid using R=301 (Permanent Redirect) while testing your mod_rewrite rules.
PS: ? after page-1 is a special mod_rewrite syntax to strip original query string. If you want to keep original query string in rewritten URL then take out ? in the end.
The problem here is that you are trying to match the query string, which has to be done by a separate RewriteCond. If you want the match specifically "Category=60", then you can add it as a Condition:
RewriteCond %{QUERY_STRING} Category=60
RewriteCond %{REQUEST_URI} /CartV3/results1.asp
RewriteRule .* home-study/A-Levels/1/page-1?
This will match http://example.com/CartV3/results1.asp?Category=60 and redirect. The ? at the end of the rule stops "?Category=60" being to the resulting URI.
If you don't care about the value in the query string, then you can remove the first condition.