skip to Main Content

I want to rewrite domain.com/pagename to /index.php?page=$pagename with this simple rule:

RewriteRule ^(.*)$ index.php?page=$1 [L]

But paradoxically I always get index.php for $1 and not pagename, why is that?

2

Answers


  1. Try this

    http://example.com/pagename => http://example.com/index.php?page=$pagename

    RewriteRule ^pagename$ /index.php?page=$pagename&%{QUERY_STRING}
    
    Login or Signup to reply.
  2. You have an infinite redirect loop, that’s why you get an unexpected result (weird you don’t get an Internal Server Error, by the way…)

    Here is a rule for what you want

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^([^/]+)$ /index.php?page=$1 [L]
    

    It checks if the url is not a physical folder/file before rewriting it internally. This rule is valid for one-level url only (eg. http://example.com/placeholder and not http://example.com/placeholder/somethingelse)

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search