.htaccess PHP - Prevent rewriting when only domain is entered - php

I have a site located at
application
controllers
views
...
public <- the root of the site
index.php <- entry point
css
...
So the url is http://localhost/application/public. I would like when the user enters only http://localhost/application/public to do nothing except calling index.php without showing it.
Here are some examples:
http://localhost/application/public -> http://localhost/application/public or http://localhost/application/public/
BUT
http://localhost/application/public/asdf -> Rewrite internally to http://localhost/application/public/index.php and show only http://localhost/application/public/asdf
I have written the .htaccess. The only problem is that when entering http://localhost/application/public it opens http://localhost/application/public/index.php instead of http://localhost/application/public
One more thing - I want existing directories to be rewritten, so only existing files should not be rewritten to index.php So basically http://localhost/application/public/css should be rewritten to index.php. The reason I want this is to ommit the 403 Forbidden error, and kindly tell the user that the path does not exist.
HTACCESS
Options +FollowSymLinks -MultiViews -Indexes
RewriteEngine On
RewriteBase /application/public/
#Remove trailing slash
RewriteCond %{THE_REQUEST} \s(.+?)/+[?\s]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/$ $1 [R=301,L]
#Do the rewriting
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [NC,L]
The reason I put RewriteCond %{REQUEST_FILENAME} !-d is to prevent http://localhost/application/public from rewriting. If http://localhost/application/public is rewritten, it loads http://localhost/application/public/index.php
Actually I know there are needed some modifications on RewriteCond %{REQUEST_FILENAME} !-d. I want the root, public folder to not get rewritten, it should be the only exception. All of the rest physical folders like css, js, have to be rewritten, like not existing. Files should not be rewritten. Thanks!

Ok, I think I found a solution.
Options +FollowSymLinks -MultiViews -Indexes
RewriteEngine On
RewriteBase /application/public/
RewriteRule ^(css/?|js/?)$ index.php [NC,L] <- Folders css, js will be rewritten, but root directory won't be rewritten. Files inside them also won't be rewritten.
#Remove trailing slash
RewriteCond %{THE_REQUEST} \s(.+?)/+[?\s]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/$ $1 [R=301,L]
#Do the rewriting
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [NC,L]

Related

How to get RewriteRule to redirect both existing and non-existing pages?

Goal: Send all requests, regardless whether the directory/file exists or not, to http://example.com/index.php
What I have tried:
Rewrite Engine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ http://example.com/index.php [L]
It redirects URL's of non-existing directories/files fine, but when I access an existing subdirectory and file http://example.com/test/test.php it only gives me the contents of the test.php page instead of the main index.php page.
If I understand correctly, the RewriteCond %{REQUEST_FILENAME} !-f and RewriteCond %{REQUEST_FILENAME} !-d tell it to perform the RewriteRule if those criterias are met, which makes sense why it only redirects on non-existing directories and files. But I've tried removing those RewriteCond's, and all it does is put my site into a redirect loop.
I've also tried doing
Rewrite Engine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*)$ http://example.com/index.php [L]
But then it doesn't redirect at all.
What do I have to put to accomplish this?
Your first example would only redirect things that does not exist to your index.php as the conditions you have there:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
Basically means if file or directory does not exist, do this.
Your second example is even worst as it tries to see if a file, directory exist and does not exist which makes no sense.
To redirect anything existent or not to a main handler in this case index.php all you need is the below:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/index.php$ [NC]
RewriteRule ^ index.php [L]
The condition makes sure you're not redirecting it to yourself to prevent a loop. If you're running HTTPD version 2.4 and above you can simple use:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteRule ^ index.php [END]
The flag [END] takes care of not executing any further redirects.

htaccess redirects not working in relative paths

I'm trying to make a redirection from all files to a template file. But redirects only works when I specify an URL
For example:
Does not work (it does not redirects)
Options -Indexes
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) /template.php?page=$1 [L,QSA]
Works (redirects to http://domain.com/versioned/template.php?page=index.html)
Options -Indexes
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) http://domain.com/versioned/template.php?page=$1 [L,QSA]
And the second option is change the URL in the browser. I need to keep the original URL.
Thanks
The problem is with
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_FILENAME} -f
If I quit this, it works, but instead of returning the current file in URL, it returns always template.php
If the htaccess file is in the versioned directory, you don't want the leading slash in the rule's target:
Options -Indexes
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) template.php?page=$1 [L,QSA]
The relative URL-path in this rule's target, template.php?page=$1, tells mod_rewrite to rewrite the request to this URL-path that is within the path that the htaccess file is in. When you had the target as /template.php?page=$1, it assumed an absolute URL-path, which would be equivalent to: http://domain.com/template.php?page=foo.

.htaccess - Redirect subfolder to index.php using subfolder name as variable

This is driving me mad. I'm trying to use .htaccess to redirect a subfolder (that doesn't exist) to the index page, using the subfolder name as the variable. ie:
http://www.website.com/john/
redirects to:
http://www.website.com/index.php?name=john
I've tried this (and various others) with no luck:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^(.*)$ index.php?name=$1
Here is an example, how you can do this:
# turn mod_rewrite engine on
RewriteEngine On
# rewrite a physical existing file or folder
RewriteCond %{REQUEST_FILENAME} !-f [OR]
RewriteCond %{REQUEST_FILENAME} !-d
# allow things that are certainly necessary
RewriteCond %{REQUEST_URI} "/layout/" [OR]
RewriteCond %{REQUEST_URI} "/javascript/"
# rewrite rules
RewriteRule .* - [L]
RewriteRule (.*) index.php?_route=$1 [QSA]
This one also denies access to folders you don't want to have public.
Try this one:
RewriteRule ^([^/]*)/$ /?name=$1 [L]
RewriteRule ^([^.]*)$ /index.php?name=$1 [L,QSA]

.htaccess all request to other file

Currently I have the following .htaccess file
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ public/index.php/$1 [L]
</IfModule>
Which works allmost perfect.
It rewerites urls like http://domain.com/something/ to the public/index.php file, like a charm, except when it is a file, just like it should.
However http://domain.com (without any path appended) (there is no index.php in the root, so it gives a 404 at the moment) is not being rewrited, how can I change this .htaccess so it rewrites this url too?
The index file is in public/index.php I want it to load that file through the use of .htaccess
Thanks
I believe to rewrite the root, you can simply do something along the lines of:
RewriteRule ^$ location/of/root/file [L]
You could try:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ public/index.php
RewriteBase should prepend the rule pattern with a leading slash, forcing it to match the root path.
Untested!
What you have there is inspired by WordPress?? It's a bad idea as it tell Apache to always check if the path is a file or a directory before redirecting.
I have something like this
RewriteEngine On
RewriteCond %{REQUEST_URI} !^.*/(css|images|javascript)(.*) [NC]
RewriteCond %{REQUEST_URI} !\.(swf|ico|php|xml)$ [NC]
RewriteCond %{REQUEST_URI} !robots.txt
RewriteRule (.*) index.php?page=$1&%{QUERY_STRING} [PT]
The first condition restricts this redirect from working in specific folders.
The seconds does it for specific extensions.
You can guess what the third does :)

Htaccess ignore folder

I'im trying to make a admin system for a site, but the htaccess ruins it D:
The admin part is in a folder named admin.
My htacces to far:
Options +FollowSymLinks -Indexes
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(admin)$ admin/index.php
RewriteRule ^([\wæøå]+)$ index.php?page=$1 [QSA]
RewriteRule ^([\wæøå]+)/$ index.php?page=$1 [QSA]
RewriteRule ^(nyhed)/([\w\d\-æøå]+)$ index.php?page=$1&nyhed=$2 [QSA]
RewriteRule ^(nyhed)/([\w\d\-æøå]+)/$ index.php?page=$1&nyhed=$2 [QSA]
But won't let me in that folder?
So I did a if in my index:
if($_GET["page"] == "admin"){
header("location:http://google.com");
}
But that won't do anything either, and no errors D:
RewriteCond lines can be strung together as you have them, but they only impact the first rule they come to.
Options +FollowSymLinks -Indexes
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?page=$1 [QSA]
This will send all requests for non-existent directories and files to index.php. Files and directories that exist won't be impacted. The other rules are unnecessary.
This is your problem:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
You are rewriting to a directory named admin which obvously exists in your file system as such Apache says:
if this path does not exist as a directory path or as a file path continue with the rewrites
That is why it is not quite rewriting the way you would expect.

Categories