tags:

views:

131

answers:

1

Hi, I have been trying to write a set of htaccess rules that will do the following:

  • With the url: mydomain.com/blog/something

    • If a file called blog.php exists in
      the web directory(the directory with index.php in) then redirect to
      blog.php?url=something

    • If blog.php does not exist, redirect to index.php?url=blog/something

    • EDIT: mydomain.com/blog/something is an example, it could be any url string and the htaccess should search the web directory for a file corresponding to the first part of the url string, which in this case is "blog"

In both cases I can then use $_GET['url'] to get parameters from the url to intiate whatever actions in my php script.

I currently have:

Options +FollowSymLinks +ExecCGI

<IfModule mod_rewrite.c>
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^([^/]*)/(.*)$ $1.php?url=$2 [L]
RewriteRule ^(.*)$ index.php?url=$1 [L]

</IfModule>

But unfortunately this is not working, I'm not sure why, I'm proficient enough with php but my htaccess skills are pretty limited. I don't know if the [L] option is the one I should be using or if I should be using more options. Currently whatever the url string is, $_GET['url'] returns "index.php".

If any more information is required just ask.

I hope someone can help Regards Luke

+1  A: 

I hope that the following directives will work for you.

Options +FollowSymLinks +ExecCGI

<IfModule mod_rewrite.c>
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/?.*$ - [E=FILE:%{DOCUMENT_ROOT}/$1.php]

RewriteCond %{ENV:FILE} !^$
RewriteCond %{ENV:FILE} -f
RewriteRule ^([^/]*)/?(.*)$ $1.php?url=$2 [QSA,L]

RewriteCond %{ENV:FILE} !^$
RewriteCond %{ENV:FILE} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>

Apache mod_rewrite Reference: http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html

Ramesh Tabarna
Hi, thanks for this answer, I'm afraid my question may not have been clear enough, what I actually want is the htaccess file to look for a file in the web directory corresponding to the first part of the url upto the "/" so "blog" was an example, I could write lots of lines in the htaccess file but if I have 10 php files in the web directory then that is a lot of repatition that I was hoping to avoid. Thanks again Luke
Luke
after some trial and error, found a suitable solution. have updated the answer, please check if it works for you.
Ramesh Tabarna
Unfortunatly it doesnt seem to work, when I have the url mydomain.com/index I get "The requested URL /web/index was not found on this server." again "index" is just an example, could this be because my web directory is not in the root of the site, another htaccess in the root of the site forwards to the web directory? Not sure if thats why I get this error.
Luke
Thank you for you response though, it very good of you to have persisted, much appreciated.
Luke
This is the htaccess in the site root. <IfModule mod_rewrite.c> RewriteEngine on RewriteRule ^$ web/ [L] RewriteRule (.*) web/$1 [L] </IfModule>
Luke
I reset the hosting for this site to look directly into the web directory and now I get from the url mydomain.com/index "The requested URL /index was not found on this server."
Luke
have updated the answer. i hope that this will finally work for you.
Ramesh Tabarna
Dude!! You are awesome, it works a treat, wish I could mark you up again. Cheers for all the effort!!
Luke