views:

29

answers:

2

well, ofcourse its not working, im still a n00b :)

this is the code that i have :

Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*)\.css - [S=7]
RewriteRule (.*)\.jpg - [S=6]
RewriteRule ^gallery gallery.html [L]

RewriteRule (.*)/(.*)/ index.html?page=$2 [L]
RewriteRule (.*)/(.*) index.html?page=$2 [L]
RewriteRule (.*)/ index.html?page=$1 [L]
RewriteRule (.*) index.html?page=$1 [L,QSA]

Now, this works fine, if i try localhost/abc but it wont work if i try localhost/abc/

also, when i try localhost/abc/def (or localhost/abc/def/ for that matter) the css file isnt being included properly. i get an error saying /abc/def/style.css does not exist. :(

however, the code doesnt work if i remove the QSA flag from the last rule. this is the code that ive come up with after a lot of googling and reading SO. if anyone can help out i'll be extremely grateful.

thanks!

A: 

Sort your trailing slash issue with (/)? rather than / alone

?=zero or one of the preceding char

Shaun Hare
hey. i tried that too. but its not working for some reason.
hsn
did you do RewriteRule (.*)/(.*)(/)?$ index.html?page=$2 [L]
Shaun Hare
yea, thats what i tried shaunhare.co.uk
hsn
A: 

You should use more specific patterns. .* means zero or more arbitrary characters. That matches anything, even the empty string.

In your case abc/ is probably matched by the rule with the pattern (.*)/(.*) and not the rule with (.*)/ as you might have assumed.

Try [^/]+ instead (one or more characters except the /) and add assertions for the start (^) and end of the string ($):

RewriteRule ^([^/]+)/([^/]+)/$ index.html?page=$2 [L]
RewriteRule ^([^/]+)/([^/]+)$ index.html?page=$2 [L]
RewriteRule ^([^/]+)/$ index.html?page=$1 [L]
RewriteRule ^([^/]+)$ index.html?page=$1 [L,QSA]

And as shaunhare.co.uk already said, you can set the trailing / optional by appending the ? quantifier to it:

RewriteRule ^([^/]+)/([^/]+)/?$ index.html?page=$2 [L]
RewriteRule ^([^/]+)/?$ index.html?page=$1 [L]
Gumbo
hey gumbo. i tried the rules you suggested. unfortunately, no matter what url i try, its getting redirected to index.html. any advice? thanks for all your help so far!
hsn
hey gumbo. after a little tweaking, i got these rules working: RewriteRule ^([^/]+)/([^/]+)(/)?$ index.html?page=$2 [L,QSA] RewriteRule ^([^/]+)(/)?$ index.html?page=$1 [L,QSA] RewriteRule ^(.*)(/)?$ index.html?page=home [L,QSA] but the css file is also getting rewritten (eg: /abc/styles.css) any suggestsions?
hsn
@hsn: You don’t need to put the single `/` in a group like `(/)`; `/?` works too. And you could use a rule that stops the rewrite process if the request can be mapped to an existing file: `RewriteCond %{REQUEST_FILENAME} -f RewriteRule ^ - [L]` Put this rule in front of your others and it should work. Then you even don’t need the first two rules.
Gumbo
hey gumbo. i tried the new rule. now its causing an internal server error :S
hsn
@hsn: You need to write each directive in its own line.
Gumbo
hey gumbo. i did infact place the lines correctly. but its still not working. i have an echo 'this page is '.$_GET['page'] in my index.html file and the css files that are being included in each page contain "this page is style.css" :Salso, can i ask how you're using the code tags in the comments? i cant find it here :P havent ever asked on SO before :D
hsn
@hsn: Did you place the rule in front of the others?
Gumbo