6

I just finished installing a LAMP stack on Ubuntu 12, and have run into an issue with Apache's .htaccess file. I have the rewrite and redirect mods enabled, and the .htaccess file is working (the URI will redirect to 'www' if there is no 'www' present), but no matter what I try, I cannot get it to remove file extensions. I've tried the <Files> directive with no luck. My current file consists of the following:

RewriteEngine On

# Remove file extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*)$ $1.php [L]

# Force www
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

Any suggestions on how to fix this very annoying problem?

0

6 Answers 6

4

You don't use htaccess to do this, you use your app to remove the extensions, and htaccess to map extension-less urls to real files. This rule

# Remove file extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) $1.php [L]

Says, "if the requested resource doesn't exist as a file, look for the resource with a .php extension". So you remove the extension from all links in your app, and this rule will make the php file run without the extension. Your htaccess is fine as-is, you need to update your app.

Sign up to request clarification or add additional context in comments.

10 Comments

I'm not really sure I follow. Update my app?
Say you have a file at "/index.php". The above rule will let you access it by requesting "/index". The rule doesn't remove the extension. You do. The rule just finds the right file with the extension removed.
That is the intended goal, yes. I originally hosted via WAMP, but decided I wanted to use Linux as a better means of hosting. I have the file in place, and it adds the 'www' just fine, but when it comes to the file extension part, it gives me a 404. I know what it SHOULD do, which is interpret all extensionless files as having a .php extension, the problem lies in the fact that it won't do just that.
Ok. Try "/$1.php" in the RewriteRule.
No, that doesn't seem to have helped. Still a 404 error when trying to access the homepage
|
2
# Apache Rewrite Rules
 <IfModule mod_rewrite.c>
  Options +FollowSymLinks
  RewriteEngine On
  RewriteBase /

# Add trailing slash to url
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/|#(.*))$
  RewriteRule ^(.*)$ $1/ [R=301,L]

# Remove .php-extension from url
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME}\.php -f
  RewriteRule ^([^\.]+)/$ $1.php 

# End of Apache Rewrite Rules
 </IfModule>

1 Comment

I set it from the original code I posted to RewriteBase / to no avail.
2

There is another htaccess alternative I use very successfully:

Options +FollowSymLinks

Options -Indexes

RewriteEngine On

RewriteRule ^purchase-jelly-babies$ /modules/products/jelly_babies.php [L]

RewriteRule ^/lets/use/an/asp/extension.asp$ /modules/test/asp_example.php [L]

This method not only solves your PHP extension issue but also allows you to keep your files organized no matter what those SEO idiots tell you what the URL should be.

Comments

0
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^\.]+)/$ $1.php 

2 Comments

Most unfortunately, this, as have all the others, returned a 404
Sure is. I even went so far as to reinstall Apache, but that has left me nowhere.
0

Please try This code and tell me it performance.

  RewriteCond %{REQUEST_FILENAME} !-d

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)/$ $1.php

RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*)/$ $1.html

RewriteCond %{REQUEST_FILENAME}.py -f
RewriteRule ^(.*)/$ $1.py

Comments

-1

the way am doing it.

RewriteEngine on
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ http://domainname.com/$1 [R=301,L]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.