.htaccess rewrite redirecting to folders - .htaccess

I am trying to remove the .php extensions from my webpage but am running into trouble. Currently, I have this in my .htaccess file:
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
RewriteRule ^(.*)$ - [L]
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^((.*/)*[^./]+)/*$ $1.php [L]
For the most part, this works. However, my site is structured so that there are folders and pages with the same name, such as example.com/page1.php
But there is also a folder page1 so that example.com/page1/subpage.php shows up like I want.
This .htaccess code correctly directs example.com/page1/subpage to the .php file, but for example.com/page1, it tries to redirect to the folder instead of page1.php, so I get an error that access is forbidden.
How can I fix this with rewrites, or should I organize/structure my files differently?

Replace your 2 rules with this rule:
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ /$1.php [L]

Related

Folders not found after mod_rewrite

I have the following project structure:
There is a folder called 'views' containing all my HTML files.
The problem now is, that my url looks like this: http://example.com/views/index, http://example.com/views/account,...
How do I use mod_rewrite in my .htaccess to get rid of the views part in the URL?
I just want that when someone visits http://example.com, it actually sees the file http://example.com/views/index.php.
I've got very close with this:
RewriteCond %{REQUEST_URI} !^/(views)
RewriteRule (.*) /views/$1
But the problem is that now all my other folders are not found anymore. My CSS from the folder css ain't found, also my media files are not found, and also not my PHP.
Edit:
This is my new .htaccess:
# browser requests PHP
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^\ ]+)\.php
RewriteRule ^/?(.*)\.php$ /$1 [L,R=301]
# check to see if the request is for a PHP file:
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^/?(.*)$ /$1.php
RewriteRule ^(css|js|media|partials|php)($|/) - [L]
RewriteCond %{REQUEST_URI} !^/(views)
RewriteRule (.*) /views/$1
It gets very close. On the third line from the bottom I was able to exclude all folders for the rewrite_rule, resulting in media, css, js,... loading. But the problem is that that line, also removes the rewrite_rule above.
In the folder php there are files like example.php, test.php,... But I refer to it without extension. But that's not working anymore because the third rule from the bottom ignores ALL rewrite_rules; so also the rules removing the PHP-extension.
So how do I exclude folders for only a specific rewrite_rule?
Edit2:
Solved.
Correct .htaccess:
RewriteEngine On
# browser requests PHP
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^\ ]+)\.php
RewriteRule ^/?(.*)\.php$ /$1 [L,R=301]
# check to see if the request is for a PHP file:
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^/?(.*)$ /$1.php
RewriteCond %{REQUEST_URI} !^/(views|css|js|media|partials|php)
RewriteRule (.*) /views/$1
Your current rule adds prefix /views to all URLs, including those starting with /css, /js, /media and so on. You need to exclude those.

What all do I need in my .htaccess, such as permissions, etc

I've got a .htaccess file at the root directory of my website, and I've made sure .htaccess is the extension, not .htaccess.txt. I'm trying to clean up my .php urls, and even after looking at all the other questions about this, it is not working.
I'm quite strongly sure that the code for removing the extension is correct, so it makes me wonder if there is other code that I need inside the .htaccess for permissions or something like that. Thanks for any answers, and please don't mark this as duplicate... I've searched for hours on Stack Overflow, SitePoint, and all the other websites.
Here is the only code inside my .htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^(.*)$ $1\.php
Just to make sure it's clear, I want the urls:
website.com/about.php
website.com/home.php
website.com/blog.php
to look like
website.com/about
website.com/home
website.com/blog
It should remove .php from ALL .php files in all directories for my website.
you actually want
RewriteEngine On
#if the requested file doesn't exist on server filesystem
RewriteCond %{REQUEST_FILENAME} !-f
#AND if the file with php extension exists. Just an extra check
RewriteCond %{REQUEST_FILENAME}\.php -f
#AND if the request isn't for domain root
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^(.*)$ /$1\.php?r=1 [L]
#redirect all urls ending with .php to "clean" URL
RewriteCond %{REQUEST_URI} ^/?(.*)\.php$
RewriteCond %{QUERY_STRING} !(^|&)r=1
RewriteRule ^(.*)\.php$ /$1? [L,R=301]
It will not make website.com/about.php look like website.com/about but it will display website.com/about.php if user visits website.com/about.

Removing .php file extension from the end of a url.

I am trying to achieve something very simple on my web server. I want urls that end in .php to end with / for example www.example.net/something.php to be www.example.net/something or www.example.net/folder/subfolder/something.php to be www.example.net/folder/subfolder/something. Additionally I don't mind if I have www.example.net/folder/something.php?something=whatever/. I am only concerned with removing the last .php in the url. I have used this code amongst many others.
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule .*[^/]$ $0/ [L,R=301]
RewriteRule (.*)/$ $1.php [L]
However this only works for some of my pages. For example it does not work on my www.example.com/folder/contactus.php page. Instead I get my 404 page. But it works on www.example/folder/faq.php perfectly.
Additionally I make some file_get_contents() php request within various parts of my code. Is there any way to tailor these rules so internal requests (my php functions) are not affected. Basically I only want the browser to display the url without the .php end without affecting my other php functions that are held in .php files. For further clarification of my intentions: I use a string processing functions to extract particular lines of text from a php file based on variables I have set up. But with the current rewrite rules the php file cannot be found because the extension has been removed from the filename (im guessing.
$file_contents = file_get_contents($filename);
I'm currently using a linux server on a shared godaddy host account.
I think you need to apply the php check to both rules:
RewriteCond %{REQUEST_URI} ^(.*)/$
RewriteCond %{DOCUMENT_ROOT}%1.php -f
RewriteRule (.*)/$ $1.php [L]
RewriteCond {REQUEST_FILENAME}.php -f
RewriteRule .*[^/]$ $0/ [L,R=301]
Additionally, the -f check needs to be made without the trailing slash
The following worked as desired, though one of my pages used get request which failed. I just changed it to a post request and all was well.
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^GET\ /([^.\ ]+\.)+php(\?[^\ ]*)?\ HTTP
RewriteRule ^(.+)\.php$ http://www.sitename.net/$1 [R=301,L]
RewriteRule ^([^/]+/)*index/?$ http://www.sitename.net/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ http://www.sitename.net/$1 [R=301,L]
RewriteCond $1 !^([^.]+\.)+([a-z0-9]+)$ [NC]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*[^/])$ /$1.php [L]
RewriteRule (.*)/$ $1.php [L]
One small problem still remains. Some pages the trailing slash is appended and others they are not. Any clues on forcing the trailing slash?

Htaccess files and url rewriting

I have 2 questions.
I am currently using wamp server to serve my website.
The homepage is 'localhost/prefix/index.php'
Question 1:
a. I would like it so my home page is:
'localhost/prefix/'
instead of
'localhost/prefix/index.php
b. I would like it so:
'localhost/prefix/admin/profile.php'
is
'localhost/prefix/admin/profile'
How do I go about doing this (I have googled and I am very confused by the syntax)?
Question 2
If I have a url like
'localhost/prefix/games?title=hi'
how can I make it so the url is like this:
'localhost/prefix/games/hi'
Thanks in advance!!
I really have got lost.
EDITED::///
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [R]
Is what I have so far.. It does nothing... But everyone says it should! (the htaccess file is doing something because if I do something random, it throws up errors).
EDITED::///
This seems to remove .php and index.php from the url:
RewriteEngine On
RewriteBase /prefix/
# remove .php; use THE_REQUEST to prevent infinite loops
RewriteCond %{THE_REQUEST} ^GET\ (.*)\.php\ HTTP
RewriteRule (.*)\.php$ $1 [R=301]
# remove index
RewriteRule (.*)/index$ $1/ [R=301]
# remove slash if not directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /$
RewriteRule (.*)/ $1 [R=301]
# add .php to access file, but don't redirect
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]
Problem now is that my prefix base is not working so it ends up going to
localhost/something/something
rather than
localhost/prefix/something/something
Any ideas?
EDITED::///
I have sussed out that the above code actually works perfectly if the page i'm directing to is in a sub folder. so for example.. this will work:
localhost/prefix/admin/dashboard
But this (because the file is in the root directory, doesn't)
localhost/prefix/login.php
it redirects me to
localhost/login
Any ideas?
EDIT::///
If you are having problems getting it to work. close your browser down and restart... I had caching issues.
This code above will remove .php and also remove index.php.

Logical AND in htaccess modrewrite?

This is my htaccess file
RewriteBase /
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
RewriteCond %{REQUEST_URI} !^/chat/
RewriteCond %{REQUEST_URI} !^/m/
RewriteCond %{REQUEST_URI} !^/__admin/
RewriteCond %{REQUEST_URI} !^/gzip_headers.php
RewriteCond %{REQUEST_URI} !^/phpfreechat/
RewriteCond %{REQUEST_URI} !^/_temp/
RewriteRule ^.+\.php$ index.php [L]
RewriteRule ^.*\.css gzip_headers.php [L]
RewriteRule ^.*\.js gzip_headers.php [L]
RewriteRule ^classifieds/ /index.php [L]
RewriteCond %{REQUEST_URI} !^/movies/.
RewriteRule ^movies/ /index.php [L]
RewriteCond %{REQUEST_URI} !^/games/.
RewriteRule ^games/ /index.php [L]
RewriteRule ^jntu/ /index.php [L]
RewriteRule ^news/ /index.php [L]
My idea behind this basically is,
forward everything to public_html/index.php (except some directories)
forward all js and css to gzip file, ( i am doing this basically because im not jsut gzipping them but also compressing in tha phpfile)
the problem is when I load images from subdirectories the are redirected to index.php as well, so just creating conditions for those directories and storing images in them like RewriteCond %{REQUEST_URI} !^/games/.
I would like to make it simple to do stuff like this
forward everything to index.php (except some conditions on top)
forward css and js to gzip file
load images and flash and some other mime types straight away only if they exists. (jpg|gif|png|swf|flv|mp4|3gp|mp3|zip|rar|exe)
Something like logical AND REQUEST_URI and -f flag I guess
Try these rules:
RewriteCond %{HTTP_HOST} =example.com [NC]
RewriteRule ^ http://www.example.com%{REQUEST_URI} [L,R=301]
RewriteRule .*\.(js|css)$ gzip_headers.php [L]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .+\.(jpg|gif|png|swf|flv|mp4|3gp|mp3|zip|rar|exe)$ - [L]
RewriteCond %{REQUEST_URI} !^/(gzip_headers|index)\.php$
RewriteCond %{REQUEST_URI} !^/(chat|m|__admin|phpfreechat|_temp)/
RewriteRule ^.+\.php$ index.php [L]
I'm not sure why your images are being redirected if your rule only redirects URIs ending with '.php'. That should exclude all other file extensions from the rule.
I'm also not sure what you mean by needing 'logical and'. When you have a number of RewriteCond lines before a RewriteRule those conditions are ANDed together and the rule is only applied if they all are true.
You can't use modrewrite to check for the existance of files and say "if the file exists, don't apply any rules, just serve up the file".
I think the best solution would be to either use a single top-level directory called 'static' or 'images' where you put all your files and exclude it from the rules, or have a wider-matching rule.
So for example you could make 'static' or 'images' a special directory name and exclude any url that contains .*/images/.* from the rules. Then /something/images/image.jpg and /something/else/images/image.jpg would both be excluded and the file would be served up.
Another hacky way would be to serve the files up from PHP. So in PHP you would translate $_SERVER['REQUEST_URI'] into a filename and see if it exists. If it does, you can write the file contents to the PHP output stream, although this won't be as efficient as leaving it up to Apache, and actually I really would not recommend it.
But like I said before, if your rule is only matching files that end with .php then your images should not be getting redirected. I would figure out why this is happening first. There is a way to turn on debug logging for mod_rewrite but you'll have to Google that.

Resources