.htaccess redirect with ignore query string - .htaccess

I would like to redirect without looking at the query string, and my redirect result no need append the query string as well, so I add a ? at the end of the RewriteRule.
I tried the following syntax, but the outcome just close to it.
RewriteCond %{QUERY_STRING} .* [NC]
RewriteRule ^exd\.asp$ http://www.example.com/index.php?r=p/consumer? [R=301,L]
and also, i tried to escape the first ?, which I need it, but still the same outcome.
RewriteRule ^exd\.asp$ http://www.example.com/index.php\?r=p/consumer? [R=301,L]
Outcome:
http://www.example.com/index.php?r=p/consumer%3f
I want to get ride of the %3f.
Thanks!

You don't need to append a ? at the end if you already have a query string in your target. Just do this:
RewriteCond %{QUERY_STRING} .* [NC]
RewriteRule ^exd\.asp$ http://www.example.com/index.php?r=p/consumer [R=301,L]
By default, query strings get appended, like this:
RewriteRule ^foo$ /bar [L]
You request /foo?blah and you get /bar?blah
However, if you have a ? in your target, query strings won't get appended unless you have the QSA, so:
RewriteRule ^foo1$ /bar? [L]
RewriteRule ^foo2$ /bar?q=2 [L]
You request /foo1?blah and you get /bar, you request /foo2?blah and you get /bar?q=2. If you include a QSA in the rewrite flags, then &blah gets appended to the end.

Related

add string at the end of URL using htaccess which has query string also

I want to change
domain.com/division1/index.php?members/maxmusterman.5
to
domain.com/division1/index.php?members/maxmusterman.5/#div
That is if the URL contains index.php?members, then I add /#div at the end of url. I tried this
RewriteEngine On
RewriteCond %{REQUEST_URI} index.php?
RewriteRule (.*) /%1/%{QUERY_STRING}&123 [L,QSA,R=301]
but it returns
domain.com/members/maxmusterman.5&123?members/maxmusterman.5
Note here that &123 is attached after URI before starting parameters. I researched htaccess QSA flag but I could not find a way to add a custom string at the end of the query string. How can I do that. Here I have used &123 for test purpose, actual requirement is adding /#div
To redirect
domain.com/division1/index.php?members/maxmusterman.5
to
domain.com/division1/index.php?members/maxmusterman.5/#div
.
You can use something like the following :
RewriteEngine on
RewriteCond %{QUERY_STRING} !loop=no
RewriteRule ^division1/index\.php$ %{REQUEST_URI}?%{QUERY_STRING}&loop=no#div [L,R,NE]
I added an additional perameter loop=no to the destination url to prevent infinite loop error .You can't avoid this as both your old url and the new url are identical and can cause redirect loop if you remove the RewriteCond and Query perameter.
NE (no escape ) flag is important whenever you are redirecting to a fragment otherwise mod-rewrite converts the # to its hex %23 .
solution #2
RewriteEngine on
RewriteCond %{THE_REQUEST} !.*loop=no [NC]
RewriteCond %{THE_REQUEST} /division1/index\.php\?(.+)\s [NC]
RewriteRule ^ /division1/index.php?%1&loop=no#div [NE,L,R]
Clear your browser cache before testing these redirects.

How to redirect all the requests to check.php except of sample.com/index.html and sample.com/?i=1

I'm trying to redirect all the requests such as example.com/123 or example.com/any-url to check.php. But I need to not redirect these two requests: example.com/ and example.com/?i=1.
Here is my .htaccess:
RewriteEngine on
RewriteRule ^(|?i=1)($|/) - [L]
RewriteRule .* check.php
But this one still redirects everything to check.php What have I done wrong?
RewriteRule ^(|?i=1)($|/) - [L]
You can't match the query string with the RewriteRule pattern. Only the URL-path (which notably excludes the query string) can be matched here, you need to use a RewriteCond directive to match the query string. (However, that regex looks invalid and I would have expected it to have generated a 500 error? That is, if it's being executed at all??)
Try the following instead:
RewriteEngine On
RewriteCond %{QUERY_STRING} !^i=1$
RewriteRule !^(index\.html)?$ check.php [L]
For all requests other than the document root (and /index.html) that do not contain the query string i=1 then rewrite to check.php.
Note that this is an internal rewrite, as opposed to an external redirect.

removing a query string

I have 60 or so old product URLS like this:
http://www.domain.com/page.php?pid=Product Name
I can rewrite the like this.
RewriteCond %{QUERY_STRING} pid=Product%20Name [NC]
RewriteRule ^page\.php /my/new/url/for/Product_Name [R=301,NE,L]
But this leaves the query string on the new url like this
http://www.domain.com/my/new/url/for/Product_Name?pid=Product%20Name
1/ Is there a way to remove the query string?
2/ Do I need to do the same for all 60 products? Fine but just a lot of copy/paste.
Thanks as ever.
If you don't care about other params:
RewriteCond %{QUERY_STRING} pid=([^&]*) [NC]
RewriteRule ^page\.php /my/new/url/for/%1? [R=301,NE,L]
If you want to save other params, use:
RewriteCond %{QUERY_STRING} ^(.*)pid=([^&]*)&?(.*)$ [NC]
RewriteRule ^page\.php /my/new/url/for/%2?%1%3 [R=301,NE,L,QSA]
This will leave a hanging ? when there are no other parameters, or a hanging & when pid is the last param on the list, but these are still valid urls.
Putting a question mark at the end of the target will clear the query string.
RewriteRule ^page\.php /my/new/url/for/Product_Name? [R=301,NE,L]
If you find yourself repeating the same pattern many times then consider using RewriteMap to specify a program to process the URL.

htacces rewrite - extend url without modifying query

I want to change:
/?q=bla
to
/search?q=bla
I have placed rule like:
RewriteRule ^search?q=(.*)$ /?q=$1 [L]
but it doesn't work, I would really appreciate some help, thanks
You can't match the query string using the pattern inside a RewriteRule. You need to match against the %{QUERY_STRING} var inside a RewriteCond:
RewriteCond %{QUERY_STRING} ^q=
RewriteRule ^search$ / [L,QSA]
Technically, you don't need the QSA flag, since query strings get appended automatically.

Rewrite trouble in htaccess

So I have a kind of strange trouble. I have a rewrite rule from
mysite.com/brand.php?brand=example&ref=ex1
to
mysite.com/brand/example/ex1
And I managed to do that, but there is also another problem: when you type this:
mysite.com/brand/example/ex1.html
or
mysite.com/brand/example/ex1.php
I get this:
mysite.com/brand/example/ex1?ref=ex1.php&brand=example
Can anyone tell why I get this redirect and how to get rid of it?
I tried this:
RedirectMatch ^brand/([A-Za-z0-9-\.\_]+)/([A-Za-z0-9-\.\_]+)php$ /brand/$1/$2
or this:
RedirectMatch ^brand/([A-Za-z0-9-\.\_]+)/([A-Za-z0-9-\.\_]+)\.php$ /brand/$1/$2
but it simply doesn't work.
Please, anyone help me!
I would use the mod_rewrite for this:
RewriteEngine On
RewriteCond %{REQUEST_URI} =/brand.php
RewriteCond %{QUERY_STRING} ^brand=([^&]+&ref=(.+)$
RewriteRule .* /brand/%1/%2? [L,R]
The RewriteRule is applied only when both RewriteCond are valid. The first RewriteCond compares the request URL to be /brand.php, the second matches regular expression against the query string and stores the matches in %1 and %2 respectively.
In the RewriteRule is the URL rewritten using the matched parts from query string.

Resources