All subdirectories to use root index.php file - apache

My existing .htaccess file looks like this:
Options -Indexes
RewriteEngine On
RewriteBase /
RewriteRule ^(.+/)?posts/(.*)/(.*).html$ index.php?content=blog&postid=$2&kwd=$3
RewriteRule ^(.+/)?aposts/(.*)/(.*).html$ index.php?content=autopost&postid=$2&kwd=$3
RewriteRule ^(.+/)?category/(.*)/(.*).html$ index.php?content=category&cat=$2&otext=$3
RewriteRule ^(.+/)?content/(.*).html$ index.php?content=$2
RewriteRule ^(.+/)?searching/(.*).html$ index.php?content=search&k=$2
RewriteRule ^(.+/)?related/(.*).html$ index.php?content=related&k=$2
RewriteRule ^(.+/)?blog/(.*)/(.*).html$ index.php?content=blog&postid=$2&kwd=$3
RewriteRule ^(.+/)?posts$ index.php?content=blog
RewriteRule ^(.+/)?sitemap\.xml$ sitemap.php [L]
RewriteRule ^(.+/)?robots\.txt$ robots.php [L]
ErrorDocument 404 /404.php
I would like to add the ability for all subdirectories to use the root index.php file. For example, if a visitor goes to the root directory:
example.com
They get served the root index.php file.
I want it so that if anybody goes to any other subdirectories eg:
example.com/sub1
example.com/sub1/sub2/sub3/etc..
They get served the index.php file from the root directory BUT the browser address bar still shows the subdirectories, eg:
example.com/sub1
example.com/sub1/sub2/sub3/etc..
Being shown how to add exceptions to this rule would be great too.
Thanks for any help.

Right under RewriteBase / add:
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond $1 !^index\.php
RewriteRule ^(.+)$ /index.php [L]
To add exceptions, just add more RewriteCond lines. For example, to make it so /foo/ doesn't get routed to index.php, add:
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond $1 !^foo/
RewriteCond $1 !^index\.php
RewriteRule ^(.+)$ /index.php [L]

Related

Hide folder in path using .htaccess file

I'm looking to hide the folder called contents in the URL path http://example.com/contents/folder-1/
My folder structure looks like:
/
--contents/
----folder-1/
------index.php
----folder-2/
------index.php
----folder-3/
------index.php
index.php
I have full root access to the server, enabled a2enmod rewrite, restarted apache. I then put the following .htaccess file in the / directory.
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+contents/([^\s]+) [NC]
RewriteRule ^ %1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (?!^contents/)^(.*)$ /contents/$1 [L,NC]
However, when I go to http://example.com/folder-1/ it just gives me a 404 page error.

In .htaccess to prioritize DirectoryIndex to a RewriteRule

I want files to be accessed when they are available as specified in the DirectoryIndex line. E.g. when someone types in www.mywebsite.com/mydir/ it will redirect correctly. For this I have in my .htaccess the line:
DirectoryIndex index.html default.php
When a filename or directory does not exist I want it to redirect to a file (target_handler.php) which is located in the same directory as my .htaccess file. For this I have placed several lines after the DirectoryIndex:
RewriteEngine On
RewriteCond %{ENV:URI} ^$
RewriteRule ^(.*)$ - [ENV=URI:$1]
RewriteCond %{ENV:BASE} ^$
RewriteCond %{ENV:URI}::%{REQUEST_URI} ^(.*)::(.*?)\1$
RewriteRule ^ - [ENV=BASE:%2]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ %{ENV:BASE}target_handler.php [L,QSA]
What I want is for Apache to first check to see if there is an index.html or default.php available, and only upon unavailability perform a rewrite. Currently the behaviour seems random(?), so if someone types www.mywebsite.com/mydir/ then Apache sometimes does the rewrite first without checking if there is an index.html in the specified folder.
Help appreciated
PS: I am testing using: xampp
Apache/2.4.17 (Win32) OpenSSL/1.0.2d PHP/5.6.15
You can omit DirectoryIndex and do it all using mod_rewrite itself:
RewriteEngine On
RewriteCond %{REQUEST_URI}::$1 ^(.*?/)(.*)::\2$
RewriteRule ^(.*)$ - [E=BASE:%1]
RewriteCond %{DOCUMENT_ROOT}%{ENV:BASE}index\.html -f
RewriteRule ^ index.html [L]
RewriteCond %{DOCUMENT_ROOT}%{ENV:BASE}default\.php -f
RewriteRule ^ default.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule !^target_handler\.php$ target_handler.php [L,NC]

Apache htaccess - change subdirectory

My file structure looks like this:
/
index.php
htaccess
cabinet
public
file.txt
index.php
I want to have a redirect from cabinet/file.txt to cabinet/public/file.txt,
and if cabinet/public/$filename does not exists then to redirect to cabinet/public/index.php instead.
How can I do this properly?
You can use a rewrite rule in Apache. Here is a good place to start if you are not familiar. What you want looks something like this:
RewriteEngine On # Turn on the rewriting engine
RewriteRule ^cabinet/file.txt cabinet/public/file.txt
ErrorDocument 404 cabinet/public/index.php
Keep in mind that the ErrorDocument will be used for all 404 or files not found.
You can use this .htaccess in /cabinet/ folder:
RewriteEngine On
RewriteBase /cabinet/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/cabinet/public/$1 -f [NC]
RewriteRule ^(.+?)/?$ public/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^((?!public/).*)$ public/index.php [L,NC]

How do I create an .htaccess to serve index if the last node is a directory, else, serve a file?

So, if I have the following URL:
http://www.example.com/Foo/Bar/Drink/
The last node of the URL (in this case, Drink) will be either a directory, in which case I want to serve index.php, or it will be a file named "Drink.php", in which case I want to serve them that file.
http://www.example.com/ would obviously serve index.php.
Both would maintain that "pretty URL" look with the trailing slash.
Is this possible? The site will follow this format consistently and I'm really trying to avoid having to route everything through the main index file.
place this code in .htaccess under the root directory of your site
Options -MultiViews
RewriteEngine ON
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule (.+) /$1/index.php [QSA,L]
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ /$1.php [QSA,L]
I think a rewrite rule might do the trick. Try something like this:
Options +FollowSymLinks
DirectoryIndex index.php
RewriteEngine On
RewriteBase /Foo/Bar/Drink
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /Foo/Bar/Drink HTTP/
RewriteRule ^Drink.php$ http://www.yourdomain.com/Foo/Bar/Drink [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /Foo/Bar/Drink/Drink.php [L]

Redirecting all requests to index.php except /files subdirectory

I have very little understanding of .htaccess files so far. I want to redirect all requests to one index.php file in my root folder, except if the url links to a file that exists and is in the files folder. (e.g. example.com/files/picture.png) I want to achieve this with only htaccess file in the root directory and if necessary one in the files directory. Moreover, i don'd want to show a permission denied message.
Right now i have this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]
But it does not redirect one to the index.php file if the file the url refers to exists. Any help? I sought on the internet but could not find anything.
Something like this:
RewriteEngine on
RewriteCond %{REQUEST_URI} /img/.+(gif|png|jpg)$
RewriteRule .* - [L]
RewriteCond %{REQUEST_URI} /css/.+(css|png)$
RewriteRule .* - [L]
RewriteCond %{REQUEST_URI} /js/.+css$
RewriteRule .* - [L]
RewriteCond %{REQUEST_URI} /js/.+(js|htm)$
RewriteRule .* - [L]
RewriteCond $1 !^(index\.php|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
All requests will go through the index.php file except for the css javascript and image files. This .htaccess file i used for the codeigniter framework, so it can be a little bit different for your situation.