.htaccess remove extension but allow 404 if not existing - php

I have removed file extensions on .php for my webserver. I just wanted to make it look nicer. This works excellent.
I have the following pages:
sitename.com/portfolio
sitename.com/portfolio/resume/file.html
The htaccess has rules so that when they visit the second link its just converting it to portfolio.php?resume&file=$1. That works awesome.
Okay heres the issue. Because i remove the extensions, if a user were to typ the domain as sitename.com/ portfolio/ with a traing slash, they will get an internal server error. Likewise, if they just type sitename.com/portfolio/r they also get an error since it doesnt exist. I know one way around this is to add an option in htacess for every page but that seems like a bad way of doing it.
Anyways here the current htacess file I am using:
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteRule ^portfolio/resumes/([^/]+).html$ /portfolio.php?resume&file=$1 [L,QSA]
RewriteRule ^portfolio/resumes$ /portfolio.php?resumes [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ /$1.php [L]
ErrorDocument 404 /404.php
If i visit the site on a page that doesnt exist like sitename.com/1 or sitename.com/1.php then I get my 404 page, but if 1.php DID exist then i would see it and IF it did exist, and I typed sitename.com/1/ or sitename.com/1/somethingelse then instead of a 404 (which I expect) then it gives me an internal server error. Is there any way around this? an error in my syntax? Thanks

This is because when you request an uri with a trailing slash eg : /uri/ , your rule rewrites it to /uri/.php instead of /uri.phpwhich is an unknown/unsupported destination. You need to accept the trailing slash as an optional char. Change your rule to :
RewriteRule ^(.*?)/?$ /$1.php [L]

Related

.htaccess ReWriteRule - Requested URL <rewritten filename> was not found on this server

I've been testing this in WAMP and I can't get it to work. I believe WAMP is set up properly due to the error message I'm receiving and it works no problem when I don't use the .htaccess file.
I have a filename that I'm using for testing called Feedback.php. Instead of displaying as www.mysite.com/Feedback.php. I'm trying to get it to just be www.mysite.com/Feedback.
.htaccess file
RewriteEngine on
RewriteRule ^Feedback.php/?$ Feedback [NC]
The error message that I receive is
"Not FoundThe requested URL /Feedback was not found on this server."
Are there two files required for reWriteRule to work?
I'm also trying to get this to work for my index.php to just be www.mysite.com which may be a different monster.
What should my navbar links be? Right now they are Feedback Should href perhaps be href="Feedback" once I get this working?
EDIT: I had the ReWriteRule variables backwards. Instead of
RewriteRule ^Feedback.php/?$ Feedback [NC]
I should have
RewriteRule ^Feedback/$ /Feedback.php [NC,L]
The rewrite doesn't look to have taken place though as it still reads localhost/Feedback.php
You may use this to remove .php extension from your pages as well as omitting the index part to your website:
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{THE_REQUEST} /index
RewriteRule ^index$ http://yourwebsite.com/ [R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]

changing .htaccess to skip .php from URL results in error for some web pages

I setup .htaccess file as follows to make elegant url by hiding .php extension;
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Now all of my pages work other than following three urls/pages;
www.auroracs.lk/about
www.auroracs.lk/enquiry
www.auroracs.lk/locate
for above URLS I get following error. (You may check online by trying that URL right now)
..............................
Not Found. The requested URL /about/.php was not found on this server.
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
..............................
But if I rename about.php to about1.php then www.auroracs.lk/about1 url works. same happens to other two php files also. Can any one help why its not working only for these three pages. Is it because my .htaccess conflicting with some other file or any advice is appreciated.
Replace your rule by this more correct rule:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ /$1.php [L]

htaccess page numbers

I'm trying to build an htaccess file that displays an index file on a blank URI, or a numeric slug (pages), such as: example.com or example.com/12 (the later appending ?page=$1)
However when accessing any non-numeric slug, it should go to a different page. For some reason, I can't get it to ever hit the index file (even when blank or numeric). What am I missing?
Here's what I have:
Options All
RewriteEngine On
RewriteRule ^([0-9]*)/?$ index.php?page=$1 [L]
RewriteRule ^([^/]+)/?$ another_page.php?slug=$1 [L]
Shouldn't a blank URI or numeric URI hit the first rule, with the Last flag, and load index.php? Every request I make is hitting another_page.php
Thanks!
Ah ha, I figured it out shortly after posting this question.
I did require the RewriteCond lines above the final, but the original files had to exist too. I setup this htaccess file before creating the index.php file (I was looking for a 404).
This is what worked:
Options All
RewriteEngine On
RewriteRule ^([0-9]*)/?$ index.php?page=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ another_page.php?slug=$1 [L]
Thanks guys!

Remove .PHP File Extension in URL

I am having a little issue forcing the .php file extension to be removed in the URL.
I am successfully able to remove .php file extension if user:
#Remove PHP if original request is /foo/bar.php
RewriteCond %{THE_REQUEST} "^[^ ]* .*?\.php[? ].*$"
RewriteRule ^(.*)\.php(\?.*)?$ $1$2 [R=301,L]
My goal is to get it also to remove the extension if:
# Remove PHP if original request is /foo.php/bar
I ask because right now a user can go to the URL and type http://www.site.com/contact.php/about and it will render my about page. My goal is force the removal of the .php and render:
http://www.site.com/contact/about
I was hoping to take the code I have above and add it to but I can not figure it out.
TIA
It looks like you got the removing part, but you're missing the internally rewriting part. What you have attempts to remove the php out of the URL and redirects the client to a URL without it. But your condition isn't matching requests, change it to:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ .*\.php.*$
RewriteRule ^(.*)\.php(.*)?$ /$1$2 [R=301,L]
Then you need to internally rewrite it back (don't redirect browser). So in the same htaccess file, add:
RewriteCond %{REQUEST_URI} ^/([^/]+)(.*)$
RewriteCond %{DOCUMENT_ROOT}/%1.php -f
RewriteRule ^([^/]+)(.*)$ /$1.php$2 [L]
the following .htaccess gives me the requested parameters, you can get "page"
AddDefaultCharset utf-8
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .* - [L]
DirectoryIndex index.php
RewriteRule ^([a-zA-Z0-9_-]{3,20})/([^/]+)/([^/]+)?$ index\.php?page=$1&s=$2&o=$3 [L]
RewriteRule ^([a-zA-Z0-9_-]{3,20})/([^/]+)?$ index\.php?page=$1&s=$2 [L]
RewriteRule ^([a-zA-Z0-9_-]{3,20})/?$ index\.php?page=$1 [L]
RewriteRule ^([a-zA-Z0-9_-]{3,20})?$ index\.php?page=$1 [L]
ErrorDocument 404 /404
Get "page" parameter and then call it like this
include('inc/'.$_REQUEST['page'].'.php');
and remember to remove .php ext from your links
Replace your tow lines with this single one : (you have an error in your rule, that's why it is not detecting .php in the middle and you don't need the rewrite condition)
RewriteRule ^(.+)\.php(/.*)?$ /$1$2 [L,R=301]
My solution for these problems is to basically avoid using complex rewrite rules and do URL routing from the php side, via a simple front controller.
Write the following .htaccess file at the root of your website:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [L]
Then write an index.php file in the same directory.
In the index.php file, you can still get the whole URL information, and choose a PHP file to include based on this.
<?php
// urldecode and get rid of the query string, $_GET is still available
$url = urldecode(preg_replace('/\\?(.*)$/', '', $_SERVER['REQUEST_URI']));
if ($url == '/contact/about') {
include 'contact.php';
}
That example is extremely basic, and I am probably ignoring subtleties of your website's architecture, but this approach is much more viable in the long run, because you can really map any URL of your liking to PHP scripts without having to endure the complexity of mod_rewrite.
This is the pattern that has been adopted by virtually every existing PHP framework (at least the MVC ones).
An minimalist example of this approach can be found in the Slim micro framework: http://www.slimframework.com/

mod_rewrite Friendly URL clashing with 404

I've been struggling with my mod_rewrite for a while now and I thought I'd cracked it…
My CMS generates blog post URLs with post.php?s= and I've been trying to remove this part of the URL so that they're nice and user friendly. Here's my mod_rewrite:
<ifModule mod_rewrite.c>
DirectoryIndex index.php index.html
ErrorDocument 404 http://tempertemper.net/error.php
RewriteEngine On
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]
RewriteCond %{HTTP_HOST} ^www.tempertemper\.net$
RewriteCond %{REQUEST_URI} !cron.php
RewriteRule ^(.*)$ http://tempertemper.net/$1 [R=301,L]
RewriteCond %{QUERY_STRING} ^s=(.+)$
RewriteRule post.php /%1? [R=301,L]
RewriteRule ^resources /archive? [R=301,L]
RewriteCond %{REQUEST_URI} !post.php
RewriteRule ^([a-zA-Z0-9_-]+)$ post.php?s=$1
</ifModule>
Unfortunately, It seems to be directing all pages that aren't found to the blank post.php page instead of the error.php page.
My site is here: http://tempertemper.net and here's a URL that doesn't exist http://tempertemper.net/this-url-does-not-exist
It should take you to http://tempertemper.net/error
Thanks for taking a look :)
Because you are redirecting all paths which fit the [a-zA-Z0-9_-]+ pattern, this will simply send to your post.php script the query string s=this-url-does-not-exist. So it's up to your PHP script post.php to check whether this-is-not-a-url exists or not, as Apache cannot possibly know what blog post ID values are valid and which are not.
As a side note, I'd recommend using /blog/2012-08-24-whatever as the path you give to visitors, rather than /2012-08-24-whatever. That way you can reserve paths for other functions, such as /images without having to write a new exception in your .htaccess file every time you need to create a new utility path.

Categories