3

I am currently trying to write a rewrite rule. I want to simplify the entered URL so that it will always use the index. The input url would be www.example.com/home.php and would be rewritten to www.example.com/index.php?r=page/home.

I wrote this .htaccess file:

RewriteEngine On
RewriteRule ^([^/]*)\.php$ /index.php?r=page/$1 [L]

This configuration unfortunately gives me an error 500. I can understand that it is caused by the fact that if I enter index.php for instance, apache will not know if it must use the index.php file or use the rewritten url index.php?r=page/index.

Is it possible to accomplish this kind of rewriting rule with apache? If so, how can I fix my error 500?

Edit: Please note that the RewriteRule works fine if I change the extension .php to anything else such as .html, as so: RewriteRule ^([^/]*)\.html$ /index.php?r=page/$1 [L]

2
  • 1
    Error 500 doesn't mean your RewriteRule is wrong or points toward a page it does not exist. It means you have an error in the syntax of your file (I don't see any) or your configuration. Is there anything else in your .htaccess file ? Does mod_rewrite is correctly installed ? Commented Apr 9, 2014 at 7:21
  • I tried changing the rewriting rule to that: "RewriteRule ^([^/]*)\.pr$ /index.php?r=page/$1 [L]" and it worked when I try to access www.domain.com/home.pr... So it is the ".php" that cause an error. Commented Apr 9, 2014 at 13:42

2 Answers 2

1

You have two possibilities.

First one

RewriteEngine On
RewriteRule ^index\.php$ - [L]
RewriteRule ^([^/]+)\.php$ /index.php?r=page/$1 [L]

Second one

RewriteEngine On
RewriteCond %{THE_REQUEST} \ /([^/]+)\.php
RewriteRule ^.*$ /index.php?r=page/$1 [L]
Sign up to request clarification or add additional context in comments.

2 Comments

I used the first one, how exactly does it work? What does the dash means?
The first one does nothing if index.php is requested (the dash means do nothing). The second one uses THE_REQUEST: this way you can make difference between internal redirect and original request. Both are equivalent in this case
1

you can also try with this :

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

Comments

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.