Apache Rewrite Directory Index - apache

Basically, if the user agent string contains something then I want DirectoryIndex to be ignored. This doesn't appear to work at all.

You can maybe try something like this:
DirectoryIndex index.html index.php
RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} !Kodi [NC]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ /_h5ai/server/php/index.php [L,QSA]

Related

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]

Mod rewrite to remove subdirectory and file extension without breaking DirectoryIndex

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]

Stop URLRewrite check after slash

Below is my current Apache configuration in .htaccess file
RewriteRule ^(user)($|/) - [L]
RewriteRule ^user/([^/]+) /user_/index.php?u=$1 [L]
I want to rewrite www.mydomain.com/user/test to www.mydomain.com/user_/index.php?u=test
But, I don't want to rewrite anything if it is only www.mydomain.com/user or www.mydomain.com/user/ where user is a directory in my server.
How can I do that?
Try this:
AddDefaultCharset UTF-8
Options -Indexes
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /?user/(.*)$
RewriteRule /?user/(.*)$ user_/index.php?u=$1 [QSA,L]
here:
RewriteCond %{REQUEST_URI} /?user/(.*)$ for check condition
You need to use quantifier .+ to make sure you don't match /user or /user/ like this:
RewriteEngine on
RewriteBase /
RewriteRule ^user/(.+)$ user_/index.php?u=$1 [QSA,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]

conditional DirectoryIndex in .htaccess

Is it possible to make the DirectoryIndex value in a .htaccess file conditional based on IP, so that - for example - my IP see's DirectoryIndex as index.html and everyone else sees DirectoryIndex as index.php?
Is there a solution other than mod_rewrite?
As far as I know, there is no conditional for DirectoryIndex. You could simulate that with a mod_rewrite directive like this one:
RewriteCond %{REMOTE_ADDR} your_ip
RewriteCond -d
RewriteRule (.*)/$ $1/index.html
If you want to exclude other visitors of the site from viewing index.html then also use
RewriteCond %{REMOTE_ADDR} !your_ip
RewriteRule (.*)/index.html$ $1/index.php
Using the provided information I beleive the following is what you need:
RewriteCond %{REMOTE_ADDR} ^your_ip$
RewriteRule (.*)/$ $1/index.php
RewriteCond %{REMOTE_ADDR} !^your_ip$
RewriteRule index.php$ index.html
So that only your IP can see index.php and everybody else will see index.html
or possibly:
DirectoryIndex index.html
RewriteCond %{REMOTE_ADDR} ^your\.ip\.000\.000$
RewriteRule ^index.html$ index.php