Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am trying to figure out Apache's Mod Rewrite and so far it's not working. Here is what I am trying to do.

I have an index.php in the root directory of my site that is a template for all pages. I want to be able to organize my files in directories and the query string will have slashes.

So...

http://www.example.com/page.html

should be...

http://www.example.com/index.php?url=page

and...

http://www.example.com/directory/page.html

should be...

http://www.example.com/index.php?url=directory/page

I've got that working but I want to be able to go n directories deep...

http://www.example.com/directory1/directory2/page.html

should be...

http://www.example.com/index.php?url=directory1/directory2/page

I know I could just put a bunch of rewrites for however many directories I deep I want to go, but is there a single line RewriteRule that will put anything after the first / will be considered a query string?

This is what I have so far...

RewriteEngine on
RewriteRule ^([a-z-]+)\.html$ index.php?url=$1
RewriteRule ^faculty/([a-z-]+)\.html$ index.php?url=faculty/$1

Behind the scenes, PHP is including the file that is in the location that the url is specified.

share|improve this question

2 Answers

this should redirect anything with a ending .html to index.php?url=anything

www.example.com => index.php?url= www.example.com/test1/test2 => index.php?url=test1/test2

        RewriteRule ^([a-z-]+)\.html$ index.php?url=$1   [L,NC]
share|improve this answer
  1. This is bad idea to include file which name was passed from user. At least implement very STRONG validation, otherwise prepare to be hacked.

  2. If you still want to use this -- here is the rule:

    RewriteRule ^(.+)\.html$ index.php?url=$1 [QSA,L]

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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