Rafee
Rafee

Reputation: 4078

.htaccess specified url with base file

I have a directory site and its sub-folder and files in it. It also contain a .htacess and below is the code

RewriteRule ^([A-Za-z0-9-]+)/?$ index.php?cc=$1

so my below url will is effective and working good.

http://localhost/site/sweetinc

And below code in index.php below

<?php
if (isset($_GET['cc']) and !empty($_GET['cc'])) {
    echo $_GET['cc'];
} else {
    die("Sorry wrong code");
}
?>

And this is working good.

Now, i want to access or display all files as normally

http://localhost/site/sweetinc/home.php
http://localhost/site/sweetinc/test.php

home.php and test.php are located in site directory. And if it redirects to other sub-folders then it should be visible like

http://localhost/site/sweetinc/sub-folder1/test3.php

Is this possible, as I am working seperating a group using seperate directory using .htaccess and considering all files in site directory as base files

enter image description here

Upvotes: 1

Views: 276

Answers (1)

Stephan Muller
Stephan Muller

Reputation: 27600

The trick here (if I understand your question correctly) is that you don't want the rewrite rule to be in effect when you're trying to reach a file that actually exists, right?

In that case, just before your RewriteRule, add these lines:

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

That way, the rewriterule only takes effect when the url (/site/sweetinc for example) does not really exist on the server. If it does (/site/sweetinc/home.php) the rewriterule is skipped and the file is shown.

(-f checks if the filename exists, -d checks if the directory exists, so /home/sweetinc/somedir/ should work too)

Update based on updated question You need two separate rules for this. First of all, if the /sweetinc/ directory in the url is used, refer them to the /site/ folder:

RewriteRule ^sweetinc/(.*)$ /$1 [L]

Then, if the file does not actually exist, let the index.php handle it:

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

Some examples:

I have the following files in my files subdomain:

  • /index.php (which has the same content as yours does, so it reads out ?cc=
  • /circle3.PNG
  • /the .htaccess with the above rules

http://files.litso.com/sweetinc/hello shows the index.php that echoes "hello" http://files.litso.com/sweetinc/circle3.PNG shows the actual file in /

(note, I don't have a /sweetinc/ directory, it's all faked)

Upvotes: 1

Related Questions