&property_type=$1&sell_type=$3&area1=$7&area2=$9&price=$11&page=$12
I'm having an issue with capture groups in a RewriteRule. $11 becomes $1's value and 1. So let's say $1 is "house", $11 ends up being "house1". Is there a way to force htaccess to get the 11th capture group rather than the first one?
Do you need all 12 capture groups?
If not you can deploy non-capturing groups (specifically intended to help you conserve backreferences).
Where the syntax for a capture group is:
(REGEX_HERE)
the syntax for a non-capturing group is:
(?:REGEX_HERE)
Related
I have a requirement to make the following paths work.
Depending on what the url consists of, they are mapped to go to different java classes.
/books/
/books/science/
/books/science/fiction/
/books/science/fiction/kids/
So, I have given the rewrite rules in my configuration file as:
^/books$
^/books/(.*)$
^/books/(.*)/(.*)$
^/books/(.*)/(.*)/(.*)$
but the moment I give a url something like this
http://localhost/books/science/fiction/kids/12345
instead of getting captured by the fourth rewrite rule, it is captured by the second one which is not what I want.
Can someone please tell me how to achieve this? Thanks in advance
^/books$ /webapp/wcs/stores/servlet/ABCController?resultsFor=allCategories [PT,QSA]
^/books/(.*)$ /webapp/wcs/stores/servlet/XYZController?make=$1&resultsFor=category [PT,QSA]
^/books/(.*)/(.*)$ /webapp/wcs/stores/servlet/ABCDController?format=$1-$2&resultsFor=subCategory [PT,QSA]
^/books/(.*)/(.*)/(.*)$ /webapp/wcs/stores/servlet/ASDFController?resultsFor=product [PT,QSA]
instead of getting captured by the fourth rewrite rule, it is captured by the second one
That’s because the dot matches any character, so slashes as well.
Replacing it by a character class allowing anything but a slash (and demanding at least one character out of that class, so + instead of *) should fix that: ([^/]+)
Another way would be to reverse the order of your rules … You should always try and write them in order from most to least specific anyway.
I've given myself a headache trying to figure out if this can be done. I have a forum that was recently migrated, leaving thousands of broken dynamic links.
A typical URL looks like this:
http://domain.com/Forum_Name/b10001/25/
('b10001' refers to the forum ID number and the last number refers to the page number.)
The new URL is formatted like this:
http://domain.com/forums/Forum_Name.10001/
(No page number. Also, notice the 'b' is no longer in front of the ID number.)
Is there a rewrite rule that can achieve this?
I'm not a rewriter, but following what I've read here, something like this should work:
RewriteRule ^([A-Za-z0-9-]+)/b([0-9])+(/[0-9]+)?/?.*$ forums/$1.$2/ [NC,L]
^([A-Za-z0-9-]+) says "begins with an alphanumeric string", then there's the /b constant, followed by [0-9]+ (one or more digits), and then an optional / with one or more digit (the page number, (/[0-9]+)?), and lastly, it ends with an optional slash (/?$).
If the URL matches that pattern, then it's rewritten to forums/$1\.$2/. \. escapes the dot (it's a wildcard), $1 is the first match of the pattern (that first alphanumeric string which is the forum name), and $2 is the second match, namely, the number after the b.
Finally, NC means pattern is case-insensitive, and L is "last" - so you don't process any other rule. I think that is most up to you, just read the linked article and pick the flags you need :)
Edit: corrected pattern checking with http://htaccess.madewithlove.be/
I think what you're looking for is
RewriteRule ^([a-zA-Z0-9_]+)/b([0-9]+)/.*$ forums/$1/$2/
Make sure the contents of the [] parts match the format you're using for forum names and ids.
For parameters, you probably want R=301 to force a permanent redirect.
I have URLs in this format
site.com/brochure/12/subcat/subcat/maincat
The only important part of the string to my application is the number directly after brochure
There can sometimes be many subcat false directories so I've had to use many rules like these to make it work
RewriteRule ^brochure/([^/]+)/([^/]+)/([^/]+)/?$ brochure.php?cat_path=$1
RewriteRule ^brochure/([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ brochure.php?cat_path=$1
RewriteRule ^brochure/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ brochure.php?cat_path=$1
etc etc - sometimes up to five different rules to allow for the different directory structures.
I'm guessing this can be done in a single rule, anyone kind enough to share their ideas?
Thanks
Since you don't care about any part of the string after your initial ([^/]+), why not just use something like:
RewriteRule ^brochure/([^/]+).*$ brochure.php?cat_path=$1
This will match and group your 12, then quietly match and discard the remainder of the string (.*$).
RewriteRule ^foo-bar-([0-9]+)-([a-z]+)-([a-z-+]+)/$ index.php?a[]=&b=$1&c=$2&d=$3&e=$4&f=$5 [L,NC]
how could i put the last parameter from rule to not be required without to add two lines and in first one to remove it, then in second to remain..?
so, rule to be in one line but to have two option to acees url like:
/foo-bar-2-steps/
/foo-bar-2-steps-eq/
One of the following:
Use two rewrite rules, one for each case
Put a question mark after the optional part, like this:
foo-bar-([a-z]+)(-([a-z]+))?
Here, the second parameter is optional. Note that this changes your numerical indexes, since you use extra parenthesis.
Menu code:
Level 1
Level 2
Level 3
Level 4
Current mod working fine on /category/18/cat-name-level/cat-name-level2/
Here is my .htaccess:
RewriteRule ^category/([0-9]+)(?:/([^/]+)(?:/([^/]+))?)(?:/([^/]+)(?:/([^/]+))?)?/$ ./category.php?pid=$1 [QSA,L]
The problem, we can type anything after the ID:
/category/18/yehahh/jsidfd/
/category/18/jkasjksd/dhgidg/ondsg/djgn/
How to fix it?
You'll have to check that the attributes after the ID correspond to the category directly in PHP, and throw a 404 if they don't (so search engines won't index the faulty URLs).
There's no way to check this directly in the .htaccess, unless you generate it and use one RewriteRule for each category.
Restrict the regex for the pid to just numbers. Something like...
\/[0-9]+?$
Then, the last thing HAS to be a numeric value. The ? is almost unnecessary here, but I always err on the side of matching less possibilities, rather than more. This solution assumes pid is an unsigned integer, of course.