I am struggling to get the $_GET parameters to sit under their respective key.
I have the following HTACCESS rule:
RewriteRule ^view/new/(.*)/?(.*)/?(.*)/?(.*)$ vehicles/new-details.php?man=$1&model=$2&trim=$3&engine=$4 [QSA]
When accessing this page, I am temporarily printing out the $_GET array:
print_r($_GET);
This returns:
Array ( [man] => BMW/3-Series/SEL/EVO-3.0-TFSI [model] => [trim] =>
[engine] => )
Whereas it should return:
Array ( [man] => BMW [model] => 3-Series [trim] => SEL [engine] => EVO-3.0-TFSI)
The man parameter is required, whereas all others are optional.
Not sure what is happening here. Could it be the fact that I have made parameters optional in the rewrite rule?
RewriteRule ^view/new/(.*)/?(.*)/?(.*)/?(.*)$ vehicles/new-details.php?man=$1&model=$2&trim=$3&engine=$4 [QSA]
(.*) will take in all characters into each capture group, you want to qualify this to take in all characters up to the /.
So replace (.*) with ([^/]*) which is telling the mod_rewrite to "Take any character until you meet a /";
RewriteRule ^view/new/([^/]*)/?([^/]*)/?([^/]*)/?([^/]*)$ vehicles/new-details.php?man=$1&model=$2&trim=$3&engine=$4 [QSA]
This should give you the result you expect.
Edit: Read here for a far fuller answer!
Related
I have urls like:
www.example.com => rewritten to www.example.com?type=default
www.example.com/foo => rewritten to www.example.com?page=foo&type=default
www.example.com/bar => rewritten to www.example.com?page=bar&type=default
and I want be able for users to use as well
www.example.com/d/ => rewritten to www.example.com?type=d
www.example.com/e/ => rewritten to www.example.com?type=e
www.example.com/d/foo => rewritten to www.example.com?page=foo&type=d
www.example.com/e/foo => rewritten to www.example.com?page=foo&type=e
www.example.com/d/bar => rewritten to www.example.com?page=bar&type=d
www.example.com/e/bar => rewritten to www.example.com?page=bar&type=e
Currently, for the first I am using simple RewriteRule
RewriteRule ^(foo|bar)$ index.php?page=$1&type=default [QSA,L]
But how can I add the support for specific types?
I can do
RewriteRule ^(d|e)/(foo|bar)$ index.php?page=$2&type=$1[QSA,L]
but is there a way how to write it in a single rule?
You can use regex for it.
"?" Makes the match optional.
RewriteRule ^(d|e)/?(foo|bar)?$ index.php?page=$2&type=$1 [QSA,L]
Reference : https://httpd.apache.org/docs/2.4/rewrite/intro.html#regex
I need to be able to match question marks because there was a translated text encoding mistake, and part of the URL ended up hardcoded with question marks in them. Here's a URL example that I need to rewrite:
https://example.com/Documentation/Product????/index.html
Here is my current rewrite rule. It works when the characters following "Product" are not question marks, but when they are, the rule doesn't apply.
RewriteRule "^Documentation/Product[^/]+/(.*)$" "https://s3.amazonaws.com/company-documentation/Help/Product/$1" [L,NC]
How would I make sure that question marks are considered to be characters too in this rule? I can't expect that only question marks and not the original non-English characters will be in the URL, so I want the rule above to match both question marks and any other character.
I found this topic which seems relevant, but the flags don't help, and the answer doesn't explain how to overcome the problem mentioned in the "Aside".
https://webmasters.stackexchange.com/questions/107259/url-path-with-encoded-question-mark-results-in-incorrect-redirect-when-copied-to
https://example.com/Documentation/Product????/index.html
You say it's "not a query string", but actually that is exactly what it is. And that is why you can't match it with the RewriteRule pattern. The above URL is split as follows:
URL-path: /Documentation/Product (matched by the RewriteRule pattern)
Query string: ???/index.html (note 3 ? - the first one starts the query string)
To match the query string you'll need an additional RewriteCond directive that checks against the QUERY_STRING server variable.
For example, to match the above URL, you would need to do something like:
RewriteCond %{QUERY_STRING} ^\?*/index\.html
RewriteRule ^Documentation/Product$ https://s3.amazonaws.com/company-documentation/Help/Product/index.html [NC,R,L]
This matches any number of erroneous ? at the start of the query string.
I've added the R (redirect) flag. Your directive (without the R flag) would trigger an external redirect anyway (because you specifying an absolute URL in the substitution), but it is far better to be explicit here. This is also a temporary (302) redirect. If this should be permanent (301) then change it to R=301, but only once you have confirmed that it's working OK (301s are cached hard by the browser so can make testing problematic).
UPDATE:
...so I want the rule above to match both question marks and any other character.
Only if there are question marks in the URL will there be a query string, so I think it is advisable to keep these two rules separate.
If there could be any erroneous characters at the start of the query string and if you want to capture the end part of the URL (like you are doing in your original directive, eg. index.html) then you can modify the above to read:
RewriteCond %{QUERY_STRING} /(.*)$
RewriteRule ^Documentation/Product$ https://s3.amazonaws.com/company-documentation/Help/Product/%1 [NC,R,L]
Note the %1 (as opposed to $1) backreference in the substitution string. This is a backreference to the captured group in the last matched CondPattern (ie. /(.*)$).
You can follow this with your existing directive (but remember to include the R flag) for more "normal" URLs that don't contain a ? (ie. query string).
NB: Surrounding the arguments in double quotes are entirely optional in this example. They are only required if you have unescaped spaces in the pattern or substitution arguments.
In summary
# Redirect URLs of the form:
# "/Documentation/Product?<anything#1>/<anything#2>"
RewriteCond %{QUERY_STRING} /(.*)$
RewriteRule ^Documentation/Product$ https://s3.amazonaws.com/company-documentation/Help/Product/%1 [NC,R,L]
# Redirect URL-paths of the form (no query string):
# "/Documentation/Product<something>/<anything>"
RewriteRule ^Documentation/Product[^/]+/(.*) https://s3.amazonaws.com/company-documentation/Help/Product/$1 [NC,R,L]
I need to substitute the character %26 for & and %3D for = in my URL:
http://www.example.com/dir/?sort-by=title%26listing_types%3Dcars
I tried the rewrite below but it does not work
RewriteRule ^dir/?$ ?sort-by=$1&listing-types=$2 [QSA,L]
Any help will be welcome
RewriteRule ^dir/?$ ?sort-by=$1&listing-types=$2 [QSA,L]
Your backreferences $1 and $2 don't actually refer to anything so these will always be empty (resulting in empty URL parameters). However, $n backreferences refer back to the RewriteRule pattern, which does not match against the query string anyway. You would need %n type backreferences that refer back to the last matched CondPattern in a preceding RewriteCond directive.
This also looks like it should ideally be a redirect, rather than an internal rewrite? Otherwise, you run the risk of duplicate content.
http://www.example.com/dir/?sort-by=title%26listing_types%3Dcars
You can do something like the following to match the above URL and replace the appropriate characters:
RewriteCond %{QUERY_STRING} ^(sort-by=.*)%26(listing_types)%3D(.*)
RewriteRule ^(dir/)$ /$1?%1&%2=%3 [R,L]
The above would temporarily (302) redirect /dir/?sort-by=<anything1>%26listing_types%3D<anything2> to /dir/?sort-by=<anything1>&listing_types=<anything2>. Change R to R=301 if this should be permanent, but only once you have confirmed it is working OK.
$1 is a backreference to the captured group in the RewriteRule pattern (ie. "dir/") and %1, %2 and %3 are backreferences to the corresponding captured groups in the preceding CondPattern in order to reconstruct the query string.
If you specifically need this to be an internal rewrite then remove the R flag (and optionally remove the slash prefix on the substitution).
http://www.example.com/dir/?sort-by=title%26listing_types%3Dcars
As mentioned in comments, you could instead handle this entirely in your server-side code. For example, in PHP you could do something like:
<?php
$queryString = urldecode($_SERVER['QUERY_STRING']);
parse_str($queryString,$urlParams);
print_r($urlParams);
?>
Given the above request URL, this will output:
Array
(
[sort-by] => title
[listing_types] => cars
)
I'm trying to figure out how to set up a mod_rewrite rule so that a request for a page, with a URL parameter appended, determines the root from which that file is served.
Here's the setup. In my "foo" directory I have "bar", "bar19", "bar27", etc.
Ideally I'd like to match on the first two characters of the "v" parameter. So like this:
I would like a request for ..................... to be served from:
www.example.com/foo/bar/something.html .......... foo/bar/something.html
www.example.com/foo/bar/something.html?v=19xxx ... foo/bar19/something.html
www.example.com/foo/bar/something.html?v=27xxx ... foo/bar27/something.html
Of course I would expect that if a value for "v" parameter that doesn't have a corresponding directory to 404.
I've done some Mod_Rewrite magic before, but I'm kind of stuck here. Any ideas?
Add a .htaccess file in directory /foo with the following content. Of course you can also insert it into your httpd.conf:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /foo
# match query parameter v with two-digit value; capture
# the value as %2 and the query string before and after v
# as %1 and %3, respectively
RewriteCond %{QUERY_STRING} ^(.*)v=(\d\d)[^&]*(&.*)?$
# match path into "bar" and rest; insert two-digit value from
# RewriteCond inbetween; append rest of query string without v value;
# use case-insensitivity
RewriteRule ^(bar)(.*)$ $1%2$2?%1%3 [NC]
</IfModule>
I think the key is to use captured values from the RewriteCond (accessible as %1, %2, etc.) and at the same time captured values from the RewriteRule itself (as usual $1, $2, etc.).
I am learning how to write regular expressions for .htaccess redirects.
So far I've managed to figure out everything I needed, except for a couple of regular expressions which don't behave as I expected. I am testing my regular expressions using a desktop application, and they work fine there, but not in the .htaccess file.
FYI: The RewriteBase is set to /site/
This is the incoming URL:
/site/view-by-tag/politics/?el_mcal_month=3&el_mcal_year=2009
I want to grab "politics" and redirect to /site/tags/politics/
Here is what I used:
RewriteRule ^view-by-tag/([a-zA-Z\-]+)/([a-zA-Z0-9\-\/\.\_\=\?\&]+) /tags/$1/ [R=301,L]
I added the capture of all the characters after politics because I am having the issue that when there is a ? in the URL the redirect does not work, and I can't figure out why. In the URL given above, if I remove the ? it works fine, but if the ? is in there, nothing happens. Is there a reason for this?
The same thing happens when I try to capture 307 from /site/?option=com_content&view=article&id=307&catid=89&Itemid=55
I used this regular expression, article&id=([0-9]+) /?p=$1 [R=301,L] but again, when there is a ? in the URL it stops the redirect for doing anything.
What is the reason for that?
The .htaccess file in question is on a Wordpress blog (3.4.1)
The point that you've missed is that the rewrite engine splits the URI into two parts: the REQUEST_URI and the QUERY_STRING. The query string part isn't used in the rule match string so there is no point in constructing rule regexp patterns to look for it.
You can probe and pick out parameters from the query string by using rewrite conditions and condition regexps to set %N variables.
By default the query string is appended to the output substitution string unless you have a ?someparam in it -- in which case it is ignored unless you used the [QSA] (query string append) parameter.
The way that you'd pick up the id in /site/?option=com_content&view=article&id=307&catid=89&Itemid=55 is to use something like:
RewriteCond %{QUERY_STRING} \bid=(\d+)
Before the rule and this would set %1 to 307. Read the rewrite documentation for more general discussion of how to do this.
The query string is must be processed separately in a RewriteCond if you need to manipulate it, and should not be matched inside the RewriteRule Instead, just match the request not including the query string, and use QSA to append the query string onto the redirect:
RewriteRule ^view-by-tag/([A-Za-z-]+)/?$ /tags/$1/ [R=301,L,QSA]
# OR, if you don't want the rest of the query string appended, put a `?` onto
# the redirect to replace it with nothing
RewriteRule ^view-by-tag/([A-Za-z-]+)/?$ /tags/$1/? [R=301,L]
Actually, the QSA may not be needed in a R redirect - I think that the default behavior is to pass the query string with the redirect.
If you need to capture 307 from the query string, do it in a RewriteCond and capture in %1:
# Capture the id in %1
RewriteCond %{QUERY_STRING} id=([\d]+)
# Redirect everything to /, pass %1 into p
RewriteRule . /?p=%1 [LR=301,L]