VladSun wrote:Code: Select all
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^event/(.*)\.php$ /index.php?event=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\.php$ /index.php?page=$1 [L]
I've tried Darhazer's suggestions and this works for me (c).
Actually, you mistyped the first rule. It should rewrite to event.php not index.php. Otherwise, yes, this is the solution. Here is the corrected .htaccess:
Code: Select all
Options -Indexes
RewriteEngine on
# Change event.php?event=party to event/party.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^event/(.*)\.php$ event.php?event=$1 [L]
# Change index.php?page=home to home.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\.php$ index.php?page=$1 [L]
But to explain why the other variations did not work, it is helpful to look at the following simplified version (which does NOT work):
Code: Select all
RewriteRule ^event/(.*)\.php$ event.php?event=$1 [L]
RewriteRule ^(.*)\.php$ index.php?page=$1 [L]
At first glance this would appear to work. However, as it turns out, the "last" [L] flag does not really mean the
last. It means the last for this pass through the .htaccess file, but what is happening is that Apache is recursively calling the .htaccess file! (Read the Apache documentation for the LAST flag) I've experimented with this a bit and have figured out a bit of what is happening.
As an example, lets say you pass in a URL = "event/myevent.php". The first pass through .htaccess this matches the first rule and the URL is rewritten to be "event.php?event=myevent". This is what we want. But for some reason (I haven't figured it out yet), Apache passes this back through the .htaccess file a second time and this time, it matches the second rule and is rewritten to be "index.php?page=event". (Note that the query string from the first pass ("?event=myevent") is discarded during this second pass through .htaccess.) But Apache is not done yet! It runs .htaccess a third time, matches the second rule again and rewrites the URL to "index.php?page=index". At this point, further passes through .htaccess make no further changes. Its almost as if Apache runs the URL through .htaccess until the URL no longer changes.
The problem, I think is that the second rule creates an infinite loop - it takes in any .php file and rewrites it to be a .php file. Thus, there is an essential need for the RewriteCond statements.
Interesting problem!