0

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 2

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)

Sign up to request clarification or add additional context in comments.

Comments

1

Try this

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

RewriteRule ^pagename$ /index.php?page=$pagename&%{QUERY_STRING}

2 Comments

thanks for answer, but actually I do not want to just match "pagename" its just a placeholder, it could also be anything else like "start", "contact" etc.
But "I want to rewrite domain.com/pagename to /index.php?page=$pagename"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.