How can I make that if the QUERY_STRING matches something it would use that rule?
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)/?$ index.php?uri=$1 [L,QSA]
RewriteCond %{QUERY_STRING} uri=admin
ReqriteRule ^admin\?(.*)/?$ index.php?uri=admin&q=$1 [L,QSA]
Eg. http://localhost/admin?poll/new
After the ? should be the paramater q, the the query would be uri=admin&q=poll/new
Any idea on how I could do this?
Thanks.
Well, it happens that your problem is more simple than the link I gave you as you do not want any analysis on the query string content.
If you use this single line:
RewriteRule ^admin index.php?uri=admin&q= [L,QSA]
Where QSA mean append the query string to the result. You will obtain an internal redirection to:
index.php?uri=admin&q=&poll/new
Which is not OK, this is because the way you use argument (admin?poll/new) is not the standard way. So it seems we'll need to capture the query string content and put it by hand on the rewriteRule. This should work (if you need it only for /admin url):
RewriteCond %{REQUEST_FILENAME} admin [NC]
RewriteCond %{QUERY_STRING} (.*) [NC]
RewriteRule .* index.php?uri=admin&q=%1 [L]
Where %1 is the first parenthesis match in the RewriteCond :(.*), meaning everything in the query string, the query string being anything after the question mark. So in fact this allows admin?poll/new but also admin?poll/new&foo=toto, giving index.php?uri=admin&q=poll/new&foo=bar
Related
Basically, I am sent a query string that I need to reformat into a URL where the values are divided by slashes and where the dots are cleared out of one of the values. I can do either of those things, but when I try to chain them together it doesn't work.
Here is the original URL
https://www.example.com/?id1=5&id2=7&id3=9&id4=7&id5=source.website.com&id6=5
Here is what I have right now ...
# Takes the dots and replaces name of server
RewriteCond %{QUERY_STRING} ^(.*source.website.com.*)$
RewriteCond %{QUERY_STRING} (.*id5=).*(id6=.*)$
RewriteRule ^(.*) %1newwebsitecom&%2?
# Rewrites Query to URL
RewriteCond %{QUERY_STRING} ^id1=(\w+)&id2=(\w+)&id3=(\w+)&id4=(\w+)&id5=([a-zA-Z0-9\.\%]+)&id6=(\w+)
RewriteRule ^(.*) https://www.example.com/dir/%1/%2/%3/%5/%6?
Separately, both of these work. However, the second rule simply takes the query and performs the actions as if the first never took place. How do I get around that?
Have you tried merging the two rules?
I've tested this rule set on htaccess.madewithlove.be and it seems to achieve the desired results.
RewriteCond %{QUERY_STRING} ^(.*source.website.com.*)$
RewriteCond %{QUERY_STRING} (.*id5=).*(id6=.*)$
RewriteCond %{QUERY_STRING} ^id1=(\w+)&id2=(\w+)&id3=(\w+)&id4=(\w+)&id5=([a-zA-Z0-9\.\%]+)&id6=(\w+)
RewriteRule ^(.*) https://www.example.com/dir/%1/%2/%3/%4/newwebsitecom/%6?
I have queries of this form going towards my server:
http://my.host.com/moo?p=1&s=2&targetURL=http://foo.com/sub/more/query
What I need to do is replace a part of the parameter of this query, specifically I need to replace http://foo.com/sub/ with http://bar.com/, preserving all the other parameters in this query and also preserving the parameters of http://foo.com/sub/more/query. I would be content with just replacing it in targetURL, so I tried this:
RewriteCond %{REQUEST_URI} ^/moo
RewriteRule ^/moo(.*)targetURL=http://foo.com/sub(.*)$ http://other.host.com/moo$1targetURL=http://bar.com$2 [L,R]
RewriteRule ^/moo(.*)$ http://other.host.com/moo$1 [L,R]
But the first query just never matches. Any help?
EDIT: To make clear, I have this:
http://my.host.com/moo?p=1&s=2&targetURL=http://foo.com/sub/more/query
and I want it to become this:
http://other.host.com/moo?p=1&s=2&targetURL=http://bar.com/more/query
Try this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^my\.host\.com [NC]
RewriteCond %{QUERY_STRING} ^(.*)targetURL=http://foo.com/sub/(.*)$
RewriteRule ^/?moo$ http://other.host.com/moo?%1targetURL=http://bar.com/%2 [L,R=301]
You need to match the targetURL=http://foo.com/sub/ part from within the %{QUERY_STRING} variable using a RewriteCond. The query string isn't part of the URI when a RewriteRule matches against it. You can then use the %1 and %2 back references to reference the groupings matched in the query string.
Try this version which i tested and works for me.
<i>
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^my\.host\.com/moo [NC,OR]
RewriteCond %{QUERY_STRING} ^p\=1\&s=2\&targetURL\=http\:\/\/foo.com/sub/more/query$ [NC,OR]
RewriteRule ^/(.*) http://other.host.com/moo?p=1&s=2&targetURL=http://bar.com/more/query/$1 [R=301,L]
</IfModule>
I am having some difficulty redirecting links. Here is a simple example and I hope you can tell me what I am missing that is causing it to fail.
RewriteCond %{QUERY_STRING} NAV=PIXIES
RewriteRule ^/category.asp?NAV=PIXIES /category/pop-artpixies-trade- [R=301,L,NE]
Please let me know what further information I can provide.
the query string isn't included in the URI for matching in the RewriteRule directive:
RewriteCond %{QUERY_STRING} NAV=PIXIES
RewriteRule ^/category.asp$ /category/pop-artpixies-trade- [R=301,L,NE]
If this is inside an .htaccess file, you need to remove the leading slash in your regexp match:
RewriteRule ^category.asp$ /category/pop-artpixies-trade- [R=301,L,NE]
If you want to actually remove the NAV=PIXIES from the query string (because the above rule will rewrite http://domain/category.asp?NAV=PIXIES to http://domain/category/pop-artpixies-trade-?NAV=PIXIES ) then you need to add a "?" to the end of your target:
RewriteRule ^category.asp$ /category/pop-artpixies-trade-? [R=301,L,NE]
Ok i am testing a cms(joomla) installed on my personal webserver before putting it live.
And i want to be able to prevent the use of the query string, or more to the point prevent users from entering stuff on the query string (changing like articleid etc), but still allow the internal redirecting to use the query string.
Example
prevent someone from entering as the url
http://www.doamin.com/index.php?option=com_user&view=register
display Error page or redirect to index.php without query string
But still allow the rewrite rule
RewriteRule ^Register$ index.php?option=com_user&view=register
RewriteCond %{QUERY_STRING} !^$
RewriteRule ^index.php$ index.php? [R=301]
obviously redirects all query strings (but it also redirects /Register which isnt what i want)
The [L] flag on the end of the Register rewriterule doesnt make it stop the rule processing either.
EDIT:
Ended up answering this with a nudge from Daniel. See answer below
The mod_rewrite documentation says:
When you want to erase an existing query string, end the substitution string with just a question mark.
And though not mentioned in this sentence, the rewrite rule must not use the QSA flag.
As far as still allowing the rewrite rule:
RewriteRule ^Register$ index.php?option=com_user&view=register
You probably want that to appear below the rewrite rule to strip the query string. When it matches, the %{ENV:REDIRECT_STATUS} variable is set to 200 and mod_rewrite runs through the rewrite rules again. The rewrite rule to strip the query string would match on this second pass if a check that %{ENV:REDIRECT_STATUS} is not 200 were not used.
This will map all requests for index.php (with or without a query string) to index.php without a query string, but still allow /Register to be treated like /index.php?option=com_user&view=register:
RewriteCond %{ENV:REDIRECT_STATUS} !=200
RewriteRule ^index.php$ index.php?
RewriteRule ^Register$ index.php?option=com_user&view=register
Or, if you want to redirect to an error page if a request for index.php has a query string:
RewriteCond %{ENV:REDIRECT_STATUS} !=200
RewriteCond %{QUERY_STRING} !=""
RewriteRule ^index.php$ error.php? [R,L]
RewriteRule ^Register$ index.php?option=com_user&view=register
But I would just use the F flag:
RewriteCond %{ENV:REDIRECT_STATUS} !=200
RewriteCond %{QUERY_STRING} !=""
RewriteRule ^index.php$ - [F,L]
RewriteRule ^Register$ index.php?option=com_user&view=register
Ok while Daniels answer did not fully work, it got me started on the right track.
ended up needing two parts to do what i wanted, part of it was using the REDIRECT_STATUS variable
first needed
RewriteCond %{QUERY_STRING} !=""<br>
RewriteCond %{ENV:REDIRECT_STATUS} 200<Br>
RewriteRule .* - [L]<br>
.....
all my internal redirects
Like: RewriteRule ^Register$ index.php?option=com_register&view=register [L]
.....
then finally
RewriteRule .* - [F,L]
Makes it so that only thing able to be used are the urls defined by the internal redirects.
the below code works to deal with my url structures but i need the rules not to work if there is parameter q= in the url.
i tried to set up a rewritecond (in the commented out line below) without success,
please help! thanks :)
Options +FollowSymlinks
RewriteEngine on
# RewriteCond %{QUERY_STRING} q!=(.*)
RewriteRule ^FSD/([^/]+)/([^/]+)/ /index.php?service=$1&type=FSD [NC]
RewriteRule ^ECD/([^/]+)/([^/]+)/ /index.php?service=$1&type=ECD [NC]
RewriteRule ^([^/]+)/([^/]+)/ /index.php?category=$1&subcategory=$2 [NC]
RewriteRule ^([^/]+)/ /index.php?category=$1 [NC]
Your RewriteCond's test pattern is a little off, but you got the right idea:
RewriteCond %{QUERY_STRING} !(\A|&)q=.*
Note that the RewriteCond only applies to the next RewriteRule, so if you want to perform this exclusion for all four of your rules, you'll need to copy the RewriteCond above each of them.