0

I have been searching all over google and stackoverflow, and have been finding a large number of answers but none seem to work for me.

Basically what I want is to rewrite my url localhost/index.php?page=1 to localhost/1, while still keeping the query alive.

Now, I have managed to remove the .php with the following code:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

But removing the Query simply does not seem to work.

Please help me out.

0

1 Answer 1

1

Your rule doesn't match what you're trying to do. You're telling it to take 1 or more of anything and make it into blah.php (like http://localhost/blah -> http://localhost/blah.php).

You're looking for something like RewriteRule ^([0-9]+)$ index.php?page=$1 [NC,L].

By the way, your language is a bit backwards. You're not rewriting index.php?page=1 to localhost/1; you're rewriting localhost/1 to index.php?page=1 (well, really 1 to index.php?page=1).

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

7 Comments

Thank you, however it is not yet working. I am receiving an 500 Internal Server Error.
@MitchelJager check your Apache error log and see what it says. RewriteRules can be a bit of a bugger to get right without a bit of trial and error. Note that you'll still need the RewriteEngine On and RewriteCond lines.
"RewriteRule: bad flag delimiters" Would be the error. I did still include the other two lines.
It seems like [NC, L] has to be written as [NC,L] (No Space). This removes the error, but it does not do anything. By doing this all I'm getting is a 404 when going to localhost/1, while localhost/?page=1 still works.
@MitchelJager was just about to say it's probably the space. Try putting a slash before the replacements: ^/(0-9]+)$ and /index.php?page=1
|

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.