Mod rewrite to remove subdirectory and file extension without breaking DirectoryIndex - apache

I have all site pages in a subdirectory like this...
http://www.example.com/pages/myfile.php
I want the URL to look like this...
http://www.example.com/myfile
Where both the subdirectory called pages and the .php file extension are removed from the URL.
My latest (partial) attempt...
Options All -Indexes +FollowSymLinks
DirectoryIndex index.php
RewriteEngine On
RewriteBase /
RewriteCond %{DOCUMENT_ROOT}/pages%{REQUEST_URI}\.php -f [OR]
RewriteCond %{DOCUMENT_ROOT}/pages%{REQUEST_URI} -d
RewriteRule ^(.*)$ /pages/$1.php [NC,L]
However, this totally breaks DirectoryIndex. When I go to http://www.example.com/ or http://www.example.com/foo/, I get a 404 error instead of defaulting to index.php as defined by DirectoryIndex.
Apparently, it treats everything as a file name instead of recognizing the lack of a file name (directory) and attempting to use index.php.
I tried incorporating this solution into mine, it fixed the DirectoryIndex issue, but it broke everything else.
Is there a solution? Please include a detailed explanation within your answer so I can learn where/how I was going wrong.

Try this in root .htaccess:
Options All -Indexes +FollowSymLinks
DirectoryIndex index.php
RewriteEngine On
RewriteBase /
# add a trailing slash if pages/<uri> is a directory
RewriteCond %{DOCUMENT_ROOT}/pages/$1 -d
RewriteRule ^(.*?[^/])$ %{REQUEST_URI}/ [L,R=302]
RewriteRule ^/?$ pages/index.php [L]
# skip all files and directories from rewrite rules below
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
# if corresponding .php file exists in pages/ directory
RewriteCond %{DOCUMENT_ROOT}/pages/$1\.php -f
RewriteRule ^(.+?)/?$ pages/$1.php [L]
# route all requests to pages/
RewriteRule ^((?!pages/).*)$ pages/$1 [L,NC]

Related

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]

All subdirectories to use root index.php file

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]

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]

.htaccess rule - check if the request is for a folder or file?

I need to check the requested URL, and serve different options depending on whether the request is for a file or a directory.
My URLs would look like:
http://www.example.com/Services/Service-1 (directory, serve /pages/Services/Service1/index.php)
http://www.example.com/Services/Service-1/Feature-1/Sub-Feature (an actual file, serve /pages/Services/Service-1/Feature-1/Sub-Feature.php)
Because of my lack of understanding of .htaccess (would this need a RewriteCondition?), I am currently stuck enumerating out each and every folder of my directory structure as follows:
RewriteRule ^Services/Service-1/(.*)$ /pages/Services/Service-1/$1.php
RewriteRule ^Services/Service-1 /pages/Services/Service-1/index.php
RewriteRule ^Services/Service-2/(.*)$ /pages/Services/Service-2/$1.php
RewriteRule ^Services/Service-2 /pages/Services/Service-2/index.php
RewriteRule ^Services/(.*)$ /pages/Services/$1.php
RewriteRule ^Services /pages/Services/index.php
RewriteRule ^Testimonials/(.*)$ /pages/Testimonials/$1.php
RewriteRule ^Testimonials /pages/Testimonials/index.php
Needless to say, this is a real pain - any time I add folders of content, I have to mess with .htaccess.
I know there must be a better way, but my google and stackoverflow searches haven't turned up anything that works when I try it.
you guessed it right, a rewriteCond can be used to verify if the requested uri is a file or a directory:
# f for a file, d for a directory
RewriteCond %{REQUEST_FILENAME} -f
you .htaccess would be:
Options -MultiViews
RewriteEngine ON
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule (.+) /$1/index.php [QSA,L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule (.+) /$1.php [QSA,L]
EDIT:
if your files reside in the page sub directory , you have to use the following code:
Options -MultiViews
RewriteEngine ON
RewriteCond %{DOCUMENT_ROOT}/pages/$1 -d
RewriteRule (.+) /pages/$1/index.php [QSA,L]
RewriteCond %{DOCUMENT_ROOT}/pages/$1.php -f
RewriteRule (.+) /pages/$1.php [QSA,L]
By far the easiest if mod negotiation is enabled (it usually is):
Options MultiViews
MultiviewsMatch Any
DirectoryIndex index.php
Won't force the .php though, if you have somefile.html as well as somefile.php the .html file is usually selected.

How can I use mod_rewrite on current directory only?

Currently I have these rules in my .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*).css style.php?u=$1 [QSA]
RewriteRule ^(.*).xml rss.php?u=$1 [QSA]
</IfModule>
This will rewrite the following URLs:
http://domain.com/user.css
http://domain.com/user.xml
But when I'm trying to grab a file from a subdirectory: http://domain.com/css/style.css it gets rewritten as well.
My goal is rewrite only for current directory and avoid sub-directories, since all real CSS files on sub-directories will be rewritten.
How I can avoid this?
You need to make your pattern more restrictive: this ^(.*).css will match ANYTHING with .css in it while this pattern ^([^/]+)\.css$ will be restricted to something.css (styles\something.css will not match it).
<IfModule mod_rewrite.c>
RewriteEngine On
# do not do anything to real files or folders
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .+ - [L]
RewriteRule ^([^/]+)\.css$ style.php?u=$1 [QSA,L]
RewriteRule ^([^/]+)\.xml$ rss.php?u=$1 [QSA,L]
</IfModule>
The easiest way is to tell mod_rewrite to avoid rewriting if the file is a real file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(.*).css style.php?u=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(.*).xml rss.php?u=$1 [L,QSA]
I've added a [L] tag (end) because once a rule is applied you certainly doesn't need the next rule to be checked.
Now if the files really exists but you really want to handle them with a php script if no 'css' subdirectory is present on the url... let's try that:
<Location "/css">
RewriteEngine off
</Location>