Is there a way with mod_rewrite to sanitize an URL of this type:
https://www.example.com/blabla/?¶m1=1¶m2=2
to
https://www.example.com/blabla/?param1=1¶m2=2
Thank you
RewriteCond %{QUERY_STRING} ^&(.*)
RewriteRule ^(.*)$ /$1?%1 [R=301,L]
That should basically do it. The RewriteCond matches on any query string that starts with &, and simply captures the rest.
And then you simply insert the back reference to that "rest" into the substitution URL, using %1.
Edit: Pattern changed to (.*) to capture all URL paths; back reference $1 inserted into substitution URL.
Related
Say I have these urls:
https://example.com/bbs/board.php?bo_table=cad
https://example.com/bbs/board.php?bo_table=videos
https://example.com/bbs/board.php?bo_table=news
How can I rewrite these in .htaccess to something like this:
https://example.com/cad
https://example.com/videos
https://example.com/news
This is my attempt thus far. I know that my rewrite method is solid because it works on URL's without query strings. I tried the QSA flag (Query String Append) to no avail.
Options -MultiViews
RewriteRule ^bbs/board.php?bo_table=cad$ /caster-cad-downloads [R=301,L,QSA]
RewriteRule ^caster-cad-downloads$ bbs/board.php?bo_table=cad [END]
RewriteRule ^bbs/board.php?bo_table=video$ /caster-videos [R=301,L,QSA]
RewriteRule ^caster-videos$ bbs/board.php?bo_table=video [END]
RewriteRule ^bbs/board.php?bo_table=news$ /news [R=301,L,QSA]
RewriteRule ^news$ bbs/board.php?bo_table=news [END]
How can I rewrite to a different URL instead of the query string while still using the
%{QUERY_STRING method?
RewriteCond %{QUERY_STRING} ^bo_table=(cad|videos|news)$
RewriteRule ^bbs/board\.php$ /%1 [QSD,R=301,L]
# RewriteRule ^(caster-cad-downloads|caster-videos|news)$ bbs/board.php?bo_table=$1 [END]
RewriteRule ^(?:caster-(cad)-downloads|caster-(videos)|(news))$ bbs/board.php?bo_table=$1 [END]
How can I rewrite these in .htaccess to something like this:
The "rewrite" is the other way round (as mentioned previously). The incoming request is for /cad and this is internally rewritten to /bbs/board.php?bo_table=cad that actually handles the request.
This can be achieved with a single rule since these 3 URLs follow the same pattern (although that conflicts with the code sample you've posted). For example:
RewriteRule ^(cad|videos|news)$ bbs/board.php?bo_table=$1 [END]
The $1 backreference contains the value of the first capturing group in the RewriteRule pattern. ie. either cad, videos or news.
The external redirect is not strictly necessary, unless you are changing an existing URL structure. Note that the RewriteRule pattern matches against the URL-path only, which notably excludes the query string. (So your rules that include a query string would never match.) To match the query string you need an additional condition (RewriteCond directive) and match against the QUERY_STRING server variable. For example, the following would go before the above rewrite:
RewriteCond %{QUERY_STRING} ^bo_table=(cad|videos|news)$
RewriteRule ^bbs/board\.php$ /%1 [QSD,R=301,L]
Note that we need to use the QSD flag here in order to discard the original query string, we don't want to append it.
The %1 backreference (as opposed to $1) matches the capturing group in the last matched CondPattern (RewriteCond directive).
Don't forget to backslash-escape literal dots in the regex in order to negate their special meaning.
UPDATE:
RewriteRule ^(cad-downloads|cad-videos|news)$ bbs/board.php?bo_table=$1 [END]
To pass cad, videos (video?) or news as the URL parameter, you could do it like this:
RewriteRule ^(?:(cad)-downloads|cad-(videos)|(news))$ bbs/board.php?bo_table=$1 [END]
This is made possible because cad, videos and news are still part of the requested URL. The outer regex group is made non-capturing (with the ?: prefix). An additional capturing group inside this captures the necessary part of the requested URL.
However, the reverse is not possible without hardcoding the mappings.
I'll see if I can get back to your other queries/chat tomorrow...
I want to redirect a URL in .htaccess, but want to keep the (dynamic) parameters from the query string at the end of the URL (e.g. ?id=1660, ?id=1661, etc.)
E.g.
https://mywebsite.example/service/viewinvoice.php?id=1660
I want to redirect it to:
https://mywebsite.example/whmcs-bridge/?ccce=viewinvoice.php?id=1660
So basically: https://mywebsite.example/service/viewinvoice.php?id=... needs to be redirected to https://mywebsite.example/whmcs-bridge/?ccce=viewinvoice.php?id=...
I tried this below, without any success
RewriteEngine On
RewriteCond %{QUERY_STRING} (^|&)/service/viewinvoice.php?id= [NC]
RewriteRule ^/?$ /whmcs-bridge/?ccce=viewinvoice.php [L,R=301]
I think this is not the right solution.
Does someone has suggestions?
You need to use the QSA (Query String Append) flag on your rewrite rule. From the documentation:
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.
From your example URLs, you don't need to match the query string in a rewrite condition. You are matching the URL path which is done as the first part of the rewrite rule itself.
Your rule should be:
RewriteEngine On
RewriteRule ^/?service/viewinvoice\.php$ /whmcs-bridge/?ccce=viewinvoice.php [L,R=301,QSA]
I have these rewrites. The first (for gallery) works as expected. The second (for photo) works but the query string is repeated. So it forwards to: http://www.domain.com/photo-TheID?id=TheID
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /gallery\.php\?set=([^/]*)\ HTTP/
RewriteRule ^gallery\.php$ http://www.domain.com/gallery?set=%1 [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /photo\.php\?id=([^/]*)\ HTTP/
RewriteRule ^photo\.php$ http://www.domain.com/photo-%1 [R=301,L]
However, if I add ? to the end of the rewriterule making it
RewriteRule ^photo\.php$ http://www.domain.com/photo-%1? [R=301,L]
It then works as expected forwarding to: http://www.domain.com/photo-TheID
My question is why is that query string being repeated without the "?" at the end? It's very confusing to me since the first rewrite (for gallery) does not have the ? yet it does not repeat the query string. If I add ? to the end of the gallery rewriterule it adds "%3f" to the end of the url.
Because it is appended automatically unless you place a trailing question mark in the substitution URL, provided it doesn't hold a new query string.
"When you want to erase an existing query string, end the substitution string with just a question mark".
Check the title Modifying the Query String in this Apache link.
Your question:
It's very confusing to me since the first rewrite (for gallery) does not have the ?
The reason is the first rewrite rule:
RewriteRule ^gallery\.php$ http://www.domain.com/gallery?set=%1
creates a new query string and in that case the incoming query string is not appended automatically unless you explicitly do it with the QSA flag.
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.
first, sorry for my bad English.
I try to rewrite url generated from Form Get and redirect that.
my url is like this:
http://www.mysite.com/properties?action=search&agreement=for-rent&category=my-category&type=&zone=my-zone&city=my-city
and I have this .htaccess configured:
11. RewriteCond %{QUERY_STRING} ^action=(?:[a-zA-Z\-]*)&(?:.*)=([a-zA-Z\-]*)&(?:.*)=([a-zA-Z\-]*)&(?:.*)=([a-zA-Z\-]*)&(?:.*)=([a-zA-Z\-]*)&(?:.*)=([a-zA-Z\-]*)$
12. RewriteRule (.*) %{REQUEST_URI}/%1/%2/%3/%4/%5/? [R=301,L]
So basically all my request are direct to index.php.
21. RewriteCond %{REQUEST_URI} !index\.php|resources|hidden
22. RewriteRule ^(.*)$ index.php/$1 [L]
All works, but the problem is when I have an empty value in query string, the rule add double slash and the above url (for example whit &type=&zone=my-zone... type have empty value) will translate like that:
http://www.mysite.com/for-rent/my-category//my-zone/my-city/
The question is: How can i remove in .htaccess the double slash generated if i have one or more empty value in query string?
Thanks
Easiest is to do another redirect (not real pretty as it requires two 301's).
RewriteCond %{THE_REQUEST} //
RewriteRule .* $0 [R=301,L]
The fun part is that when the url is loaded with a double slash in it, mod_rewrite will automatically remove this. So as you can see above you'll just have to rewrite the url to itself, kind of.