Darcey
Darcey

Reputation: 1987

htaccess multiple conditions and rewrites

I am trying to process the following:

  1. All urls go to www.mydomain.com/index.php
  2. GET paramaters are still processed
  3. Removal of .php

    So if I have: www.mydomain.com/test.php?a=1 I want to check here for the existence of file app/controllers/test.php and pass the string app/controllers/text.php to ./index.php and remove .php from test.php

    or

    If I have: www.mydomain.com/dir1/test.php?a=1 I want to check here for the existence of file app/dir1/controllers/test.php and pass the string app/dir1/controllers/text.php to ./index.php and remove .php from test.php

    or

    If I have: www.mydomain.com/dir1/dir2/dir3/test.php?a=1 I want to check here for the existence of file app/dir1/dir2/dir3/controllers/test.php and pass the string app/dir1/dir2/dir3/controllers/text.php to ./index.php and remove .php from test.php

As you can see sub dir needs to be dynamic so DIR and FILE check is necessary.

So.. If it php file test.php exists at any directory level

  1. Remove .php from browser url and send param to www.mydomain.com/index.php, so that I can pickup the path to the file but the browser never sees file extension

If it doesn't exist (this part I already have working) I want to proceed and run what I already have in my htaccess. Which will assume index.php at directory path and redirect to a 404 if not found

I would expect it to look something like this:

Options +FollowSymlinks
RewriteEngine On

#Rewrite Conditions to detect actual file at path adding app/controllers 
#to front of url string as this shouldn't be viewable
# -- CODE I AM STUCK WORKING OUT --
# [L] to prevent proceeding with htaccess if condition met and rewrite done

# Existing code below to run if above not found
#RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
#RewriteRule ^(.*)$ ./index.php?p=$1 [L,NC,QSA]

Thanks

D

Upvotes: 0

Views: 265

Answers (1)

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324790

To get the .php stripped from the URL itself, you need a redirect:

RewriteRule (.*)\.php$ $1 [QSA,R=301]

Now to apply your actual logic, I think this should work:

RewriteRule (.*)/([^/]+)$ index.php?file=app/$1/controllers/$2.php [QSA]

You will then have to have PHP check if( file_exists($_GET['file'])), because your condition is too complex to check in .htaccess (inserting that controllers/ into the path to check)

Upvotes: 1

Related Questions