What I am attempting to do, is have pretty URLs. I have achieved this for the case where I want the entire string to point to one PHP script. I do not.

So, this works: example.com/login/1/2 => /index.php?go=login&do=1&msg=2


RewriteEngine on
RewriteBase /
RewriteRule ^([0-9a-zA-Z_-]+)/?$ /index.php?go=$1 [L,QSA]
RewriteRule ^([0-9a-zA-Z_-]+)/([0-9a-zA-Z_-]+)/?$ /index.php?go=$1&do=$2 [L,QSA]
RewriteRule ^([0-9a-zA-Z_-]+)/([0-9a-zA-Z_-]+)/([0-9a-zA-Z_-]+)/?$ /index.php?go=$1&do=$2&q1=$3 [L,QSA]

Now let's say I want example.com/webapp/1/2/3 => webapp.php?1=1&2=2&3=3

By adding this below the above:

RewriteRule ^webapp/?$ /webapp.php [L,QSA]
RewriteRule ^webapp/([0-9a-zA-Z_-]+)/?$ /webapp.php?go=$1 [L,QSA]
RewriteRule ^webapp/([0-9a-zA-Z_-]+)/([0-9a-zA-Z_-]+)/?$ /webapp.php?go=$1&do=$2 [L,QSA]
RewriteRule ^webapp/([0-9a-zA-Z_-]+)/([0-9a-zA-Z_-]+)/([0-9a-zA-Z_-]+)/?$ /webapp.php?go=$1&do=$2&q1=$3 [L,QSA]

Navigating to example.com/webapp redirects to index.php every time, stripping any query string.

If I comment out the first code block, and navigate to example.com/webapp I get webapp.php as I should.

I get no errors in any log. The access log during the call reports 302 to webapp

What am I doing wrong?

  • "The access log during the call reports 302 to webapp" - The above rules won't trigger a 302, so you must be looking at something else in your logs? (In fact, you shouldn't be getting a 302 or any kind of redirect related to this, if the URLs are being linked correctly?) – MrWhite Sep 9 '16 at 11:50
  • I have no idea about the 302, But here is the log from a request to /webapp before I listened to you and changed the order of the rules. - - [09/Sep/2016:01:02:18 -0500] "GET /webapp HTTP/1.1" 200 734 "-" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:48.0) Gecko/20100101 Firefox/48.0" - - [09/Sep/2016:01:02:28 -0500] "GET / HTTP/1.1" 302 546 "-" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:48.0) Gecko/20100101 Firefox/48.0" – Steven Gunn Sep 10 '16 at 20:24

by adding this below the above

You need to add that before the above!

Your rules need to be arranged in order of specificity. The more specific rules first, the most general last. Your first rule block contains the most general / catch-all rules, so that is what catches the request and is where processing stops.

  • I will give this a try. Should I also reorder each line from what it is, so bigger to smaller? – Steven Gunn Sep 9 '16 at 1:46
  • You were exactly correct, moving my rules around as suggested solved my problem. – Steven Gunn Sep 9 '16 at 6:08
  • "also reorder each line from what it is, so bigger to smaller" - that is not necessary in this instance, since there is no conflict... the first rule matches exactly 0 params, the second exactly 1 param, the third exactly 2 params. Reordering these would make no difference. However, if the second rule matched at least 1 param (so, it would also match 2 params, etc.) then yes, you would need to reorder these directives - but that is not the case here. – MrWhite Sep 9 '16 at 11:47

I am not sure if you got the concept of pretty URLs right: https://en.wikipedia.org/wiki/Semantic_URL What you're planning to do definitely won't help your SEO. And you shouldn't redirect temporarily (302 redirect) but permanent [R=301].

  • Thanks for the insight. This is a intranet type application and seo won't be a factor. However I need it to be simple urls for link sharing. I agree with 301 vs 302. I'm not sure how to switch it as it sits. But ill research it. – Steven Gunn Sep 9 '16 at 1:44
  • I'm curious why you think the OP might not have gotten "the concept of pretty URLs"? "won't help your SEO", but it wouldn't necessarily hamper it either (if SEO was an issue)? "you shouldn't redirect temporarily (302 redirect) but permanent [R=301]" - There shouldn't be any redirects (except to redirect already indexed/linked content). The directives shown in the question are internal rewrites, not external redirects, so it's difficult to say what the "302" that the OP is quoting from the access logs is referring to. (?) – MrWhite Sep 9 '16 at 21:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.