My .htaccess is as follows:
RewriteEngine On
RewriteRule ^client/([0-9a-zA-Z]+)/(.*)$ licensee/client/$2?id=$1 [L,QSA]
RewriteRule ^licensee/(.*)/$ licensee/$2?id=$1 [L,QSA]
If I visit /client/asdf/ in my browser however, it uses the second rewrite rule instead of the first. If I comment out the second rewrite rule, it uses the first rule as expected.
I can't figure out how the second rule could possibly match /client/asdf/ - have I overlooked something?
When you visit /client/asdf/, it matches the first RewriteRule, so a new request becomes "licensee/client/?id=asdf" which matches your second rule.
Related
Okay, so I have two separate mod-rewrite rules in my vhost block. The first rule redirects a customer offsite if they come in thru an affiliate URL such as example.com/1234.html and the second rule forces the URL to always contain www dot like www.example.com.
# Affiliate Links
RewriteRule ^([0-9]+)\.html$ http://affiliates.example.com/log.php?id=$1 [R=302,L]
# Ensure we are always on www dot
RewriteCond %{HTTP_HOST} ^example\.loc [NC]
RewriteRule (.*) http://www.example.com$1 [R=301,L]
The rules themselves work great. The problem is that if the first rule applies I want it to immediately redirect, however it seems as if the second rule is hoisted to the top because it always takes precedence. What do I need to change so that these execute in order?
You've stated that this is in a vhost block. In that context (as opposed to, for example, an .htaccess file) URLs always start with '/'
Thus
RewriteRule ^([0-9]+)\.html$ http://affiliates.example.com/log.php?id=$1 [R=302,L]
should instead be
RewriteRule ^/([0-9]+)\.html$ http://affiliates.example.com/log.php?id=$1 [R=302,L]
(ie, with the leading slash), otherwise it will never match anything.
We have the following rewrite rule:
RewriteRule ^([A-Za-z0-9\_]+)$ index.php?rewrite=$1 [L,QSA]
We were wondering if there was a way to have the ?rewrite=$1 take precedence over one that is passed via the query string in the request uri?
Because as it stands now, due to the QSA flag (which we do need btw) if the following url is hit:
http://www.domain.com/this_rewrite_will_match_the_above_rule?rewrite=some_value
The value of $_GET['rewrite'] in PHP will be some_value, and not this_rewrite_will_match_the_above rule.
Before we go in and start modifying the rewrites and adding a RewriteCond to match the query string, etc etc... We were hoping there was a flag to set so that the target url (index.php?rewrite=$1 in this case) took precedence over the passed query string values.
Hope this makes sense.
Thanks.
Slightly hackish
RewriteRule ^([A-Za-z0-9\_]+)$ index.php?%{QUERY_STRING}&rewrite=$1 [L]
This works because, in php, the second rewrite=... overwrite the first.
I have searched for some mechanics to override single querystring parameters in an Apache rewrite multiple times and quite intensely, but it looks like there is no option to do that, even with the latest Apache version (2.4.3 at the time of this writing).
But there is an alternative that makes use of the fact that the PHP querystring parser only returns the last of multiple idententical querystring parameters.
Example:
http://www.domain.com/index.php?id=123&id=456
This will return the following single (!) entry in $_GET:
array(1) {
["id"]=>
string(3) "456"
}
So you can solve your problem by simply appending any override parameters to the end of your existing querystring (without removing them within the querystring). The last occurrence of a repeated parameter is the one that makes it into the $_GET array.
Unfortunately the QSA switch is not suitable for this technique, as it always appends the original parameters to the end of the new querystring. There is no switch that would preprend the old parameters. So you have to take a little detour with a RewriteCond to catch and prepend the original querystring yourself instead of using QSA:
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^([A-Za-z0-9\_]+)$ index.php?%1&rewrite=$1 [L]
The only function of the RewriteCond is to capture the querystring in %1. The regexp (.*) of the condition is always matched, so the following RewriteRule is always executed.
With this technique your above testcase will rewrite to...
http://www.domain.com/index.php?rewrite=some_value&rewrite=this_rewrite_will_match_the_above_rule
...which will be interpreted by the PHP querystring parser to
$_GET["id"] => "this_rewrite_will_match_the_above_rule"
...which is what you want.
Please be aware that this will work only if you take your querystring values from PHP's $_GET array. It will not necessarily work if you parse the content of $_SERVER["QUERY_STRING"] yourself or if you use any other programming language.
I have opted to create an answer of my own, because its slightly cleaner then the examples provided by Jpsy and Gerben.
Credit where credit is due, their suggestions are what got me here, I only expanded on them:
So, our final solution includes 2 rules.
# check if querystring is not empty (this is the addition vs other answers)
RewriteCond %{QUERY_STRING} !^$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([A-Za-z0-9\_]+)$ index.php?%{QUERY_STRING}&rewrite=$1 [L]
if the above query string fails (mainly, the querystring is empty) this rule will apply.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([A-Za-z0-9\_]+)$ index.php?rewrite=$1 [L]
The reason I opted to go for this dual rule setup is to avoid having the php server variables polluted with "?&rewrite=...." if the query string is empty.
Thanks to jpsy and gerben for the help.
I used the following instructions:
RewriteRule ([^\?]*)\?(.*) $1?$2 [N] (1)
RewriteCond %{QUERY_STRING} (.*?)&?(rewrite=[^&]+)&?(.*)
RewriteRule ^(.*)$ $1?%1&%3 [N] (2)
RewriteRule ^(.*)$ $1?domain=newValue [L,QSA] (3)
The trick is done by the [N] flag in the first two rules, which causes to rewrite engine to re-process the output.
Rule (1) simple rewrites the url as it is. I needed it because I'm using mod_rewrite togheter with mod_proxy_ajp and in the first iteration the query string is not splitted from the url. After the execution of the first line, the url is unchanged but the engine will split the path from the query string.
Rule (2) iterates and removes all occurrences of the parameter "rewrite" from the query string.
Rule (3) sets the new value for the parameter and appends whichever query string survives from the replacement done by rule (2).
I want to have RewriteRule where domain's /page/test or /page/test/ go to www.domain.com. Only for this two example.
I try:
RewriteRule ^/page/test/?$ http://www.domain.com [R=301,L,NC]
This will be good but I have other pages like /page/test/?D_test
and when I put RewriteRule, redirect also /page/test/?D_test. Problem is '?'. How should I write my rule to don't rewrite another page's to www.domain.com ? for example
/page/test/?D_test /page/test/(.*)
I'm not sure I understand your question. You are either asking how can I redirect all the traffic including the query string to the domain and take off the query string. In that case I usually do it this way:
RewriteRule ^/page/test/?$ http://www.domain.com? [R=301,L,NC]
If you want to make it so it doesn't redirect if there is a query string then you have to use a rewrite conditional.
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^/page/test/?$ http://www.domain.com? [R=301,L,NC]
I'm trying to transform "domain.com/index.php?site=food&category=beef" into "domain.com/food/beef" but it does not work, no matter what I try. It always leaves the original domain and I get no errors.
I think it's my fault, I tried this for 3 different URLs on 3 different servers (and 3 different projects)... it just seems like I don't get how mod_rewrite really works, though I read every documentation on this topic I found. I even spent days here on SO without finding any solution.
Mod_rewrite is enabled on the server:
RewriteEngine On
RewriteRule ^ http://www.google.com [R,L]
gives me "http://www.google.com/?site=food&category=beef". It looks like mod_rewrite does not recognise the query string... So I tried several solutions with RewriteCond %{QUERY_STRING}... but nothing works :/
Hopefully you guys can help me! I'm going insane on this!
Thanks in advance!
Try:
RewriteEngine on
RewriteRule ^food/beef$ index.php?site=food&category=beef [L]
Or more generally:
RewriteEngine on
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ index.php?site=$1&category=$2 [L]
Are you trying to do something like this?
RewriteRule ^([^/]+)/([^/]+)/? /index.php?site=$1&category=$2 [L]
This will make it so when you go to http://domain.com/food/beef the request gets rewritten to "/index.php?site=food&category=beef" internally and index.php is used to serve the original request. The browser's location bar will still say "http://domain.com/food/beef".
If you want the location bar to say http://domain.com/index.php?site=food&category=beef then add an "R," to the "[L]". If this is backwards and you want it so when someone enters http://domain.com/index.php?site=food&category=beef in the location bar, and the request gets rewritten to "/food/beef" internally on the server, then you need to parse out the query string using RewriteCond:
RewriteCond %{QUERY_STRING} ^site=([^&]+)&category=([^&]+)
RewriteRule ^index.php /%1/%2? [L]
The same thing applies with the "R" causing a browser redirect like the first example. If you want the location bar to change to http://domain.com/food/beef then the brackets should look like: [L,R]. Note that you need a ? at the end of the target in the rule, so that query strings don't get thrown in. That is why in your google example, the query string is being appended.
EDIT:
Seeing as you just wanted to change what's in the browser's location bar and not where the content is:
You need to re-rewrite what the 2nd rule above has rewritten BACK to index.php, but without a redirect. In order to keep the 2 rules from looping indefinitely because one rule rewrites to the other rule and vice versa, you need to add a flag somewhere to keep the 2nd rule above from redirecting you over and over again.
So combining the two, you'll have this:
RewriteRule ^([^/]+)/([^/]+)/? /index.php?site=$1&category=$2&redirected [L]
RewriteCond %{QUERY_STRING} !redirected
RewriteCond %{QUERY_STRING} ^site=([^&]+)&category=([^&]+)
RewriteRule ^index.php /%1/%2? [L,R=301]
Note the redirected parameter in the query string. This gets inserted when someone tries to access the clean version of the url, e.g. "/food/beef". internally, it gets rerouted to index.php but since the rule doesn't have a "R", the browser's location bar doesn't change.
The second rule now checks if the request contains the redirected param in the query string. If it doesn't, that means someone entered in their browser's location bar the index.php url, so redirect the browser to the clean version.
Hi. I am new to mod_rewrite and was wondering if it was possible to do the following:
RewriteRule ^([^/]*)$ index.php?slug=$1
This rule will point only at index.php but if I wanted to do another rule that pointed a specific slug to a different script, i.e.
RewriteRule ^a-new-page$ different.php
This would not work because the first rule has declared anything that is entered should point at index.
Is there a way to force the new rule for that specific slug?
Yes, it is possible -- just place such specific rule before generic/broad one (the order in which rules are declared matters):
RewriteRule ^a-new-page$ different.php
RewriteRule ^([^/]*)$ index.php?slug=$1
Also please pay attention to the broad rules -- they may enter into a rewrite loop (after rewrite new URL goes into next iteration, and if rule is not written right it may enter endless loop which Apache will have to forcedly terminate and your user will see an 500 error page instead). Better rule will be:
# specific rule
RewriteRule ^a-new-page$ different.php [L,QSA]
# broad rule (will be triggered if requested resource is not a file or directory)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)$ index.php?slug=$1 [L,QSA]
[L] tells Apache to stop rewriting if this rule matches
[QSA] tells to append query string to a new URL (useful if you have URL parameters: e.g. tracking data, page parameters etc).
Useful Link: http://httpd.apache.org/docs/current/rewrite/