Complex htaccess rewrite rules - where do i start? - apache

I have asked on here before about mod_rewrite and got the perfect answer but now I need to do something more complex and really needed the advice of someone who knows mod_rewrite much better.
My rewrite rule looks like this so far:
RewriteRule ([a-zA-Z_-]+)/([0-9]?) index.php?cat=$1&page=$2
And that shows the URL as follows: /categoryname and shows pages like /categoryname/1/.
But I would really like to modify the rule to work with sub categories such as /categoryname/subcategoryname/ and still work with the page system (subcat/2/) but I just can't seem to wrap my head around this.
It needs to ignore the sub cat rewrite if you are on the main category etc.
Hoping someone can help me.

RewriteRule [a-zA-Z_-]+/([a-zA-Z_-]+)/([0-9]?) index.php?cat=$1&page=$2 [L]
RewriteRule ([a-zA-Z_-]+)/([0-9]?) index.php?cat=$1&page=$2 [L]

I would use these rules:
RewriteRule ^([a-zA-Z_-]+)/([a-zA-Z_-]+)/(([1-9][0-9]*)/)?$ index.php?cat=$1&subcat=$2page=$4
RewriteRule ^([a-zA-Z_-]+)/(([1-9][0-9]*)/)?$ index.php?cat=$1&page=$3
With the assertion for the start (^) and the end of the string ($) there is no ambiguity to have one of those rule accidentally applied. And the (([1-9][0-9]*)/)? part is for the optional paging.

Related

confused with Mod rewrite RewriteRule

everything at my website is served from a master index.php at Document root
so normally all the functions looks like ./index.php?storage=seven
i wanted to make it looks better, so tried to use Mod_Rewrite as shown below
RewriteEngine On
RewriteRule ^/storage /index.php?storage=seven [L]
Now i am able to access the function using ./storage
but when somebody types ./storage/ (an extra '/' at end) or ./storage/some/junk the Link still works but the path to images/css/script in my page brakes(the paths to these is broken), which means, i just need it to be working for ./storage and everything else should give 404 error (./storage/ and ./storage//... to give 40)
Please help me out here, i am very new to Mod_reWrite
Thanks
Small change in rule ... and it started working
RewriteRule ^storage$ index.php?storage=seven [L]
By the way seven here is hard-coded from the Rule itself, please don't get confused with that
Thanks

Using .htaccess mod_rewrite to pass all URLs in a given directory to a single redirect script

Im trying to use mod_rewrite to redirect any call to /real-estate/* to rewrite.php...i know i can redirect everything to rewrite.php with this:
RewriteRule ^(.*)$ rewrite.php?url=$1 [L]
I would like to have my urls formatted like /real-estate/12345/123-anywhere-st ....where the 123-anywhere-st would be ignored, and have /real-estate/12345 sent to rewrite.php...id like the rewrite rule to only be used on /real-estate...all other areas of the site should function as is...Ive searched all over for a good tutorial or cheat sheet, but none that I can find actually explain how to format the mod_rewrite rules, they just give one or two examples and thats it...can anyone help, as well as maybe provide a link to somewhere I can learn
Thanks!
RewriteRule ^/real-estate/(.*)$ rewrite.php?url=$1 [L]

simple(?) htaccess rewriting issues

OK I have been messing about with URL rewriting for the last few days and seem to have come to a bit of a dead-end. I have come up with a few solutions that work on some servers and not others, and my hosting company (1and1 - be vary wary of these guys if you choose them as hosts) hasn't been able to help at all.
My problem is this, i want to re-write this URL:
/result.php?section=[section name]&url=[url]
to this (adding a trailing slash if there is none):
/article/[section name]/[url]/
and ALSO
section.php?section=[section name]
to (again adding a trailing slash):
/section/[section name]/
each attempt seems to have different results. currently I am using thhe following, which works locally, but on the live server only the 'articles' rewrite works:
RewriteRule ^article/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ /article/$1/$2/
RewriteRule ^article/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/$ /result.php?section=$1&url=$2
RewriteRule ^section/([a-zA-Z0-9_-]+)$ /section/$1/
RewriteRule ^section/([a-zA-Z0-9_-]+)/$ section.php?section=$1
Can anyone help me come up with a solution that will work nicely? Thank you in advance, I am really struggling with what seems like something relatively straightforward...
Firstly in order to force the backslash on the end you will need to actually do a redirect...
RewriteRule ^article/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ /article/$1/$2/ [R=302,L]
RewriteRule ^section/([a-zA-Z0-9_-]+)$ /section/$1/ [R=302,L]
Note: I use 302 because 301 can be a pain during testing, once everything is working and you are happy with it change it to 301.
The two remaining rules are the rewrites that simply mask/alias the query_string URL, which should look something like this...
RewriteRule ^article/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/$ /result.php?section=$1&url=$2
RewriteRule ^section/([a-zA-Z0-9_-]+)/$ /section.php?section=$1

Apache Rewrite - put parts of query string in replacement string

I'd like to rewrite:
www.example.com/file.html?username=john&number=1234
To:
www.example.com/users/john
But I can't figure out how to extract the "username" value from the query string. I've been Googling this all morning and reading the official docs but no luck. I need to solve this problem with a rewrite, rather than changing the application.
Any help much appreciated!
Rangi
RewriteCond %{QUERY_STRING} username=([^&]+)
RewriteRule /?file.html /users/%1
Going to http://example.com/file.html?username=foobar will then redirect you to http://example.com/users/foobar, add an [R] to the end if you need an external redirect.
Mostly the rewrites are done the other way around, it's rare to see someone who wants a querystring in 'outside' urls but doesn't have them internally. Or did I understand your question backwards?
Ok I've solved this using two rules, although not sure if I'm doing it the best way.
RewriteRule ^file.html xxx/%{QUERY_STRING} [L]
RewriteRule ^xxx/[^=]*=([^&]*) /users$1 [R=301,L]
The first rule makes the query string part of the URL, so the second rule can see it, and therefore match and rewrite parts of it. I used "xxx" but it could be anything.

How to write this URL rewrite rule?

Using LAMP, is it possible to write rewrite rules to redirect URLs like the following?
http://example.com/topic/142 -> http://example.com/static/14/142.html
--Edit--
The rule is to get ID's first 2 numbers as folder name, then ID.html.
Try this rule:
RewriteEngine on
RewriteRule ^topic/(([0-9]{2})[0-9]*)$ static/$2/$1.html
Is it possible, yes, surely.
RewriteRule /topic/(.+) /static/14/$1.html
However, this will give you the /14/ part every single time. As long as you don't have a hint were this part is encoded in your original URL, there is no way to change this.
RewriteEngine on
RewriteRule ^(([0-9]{1,2})[0-9]*)$ /$2/$1.html
Greedy matching means that the first selector will pick up two characters if they are available.
However, I'm not sure that your rule makes much sense, as pages 14, 140-149 and 1400-1499 will be in the same directory. Might it make more sense to put 0-99, 100-199, etc in the same directory?
RewriteEngine on
RewriteRule ^([0-9]{1,2})$ /0/$1.html
RewriteRule ^(([0-9]+)[0-9]{2})$ /$2/$1.html