Reputation: 1852
I want a mod_rewrite rule set, so I can refer to a page without the .php extension, but have that rewritten to include the .php extension. This will be running on a 1&1 server.
Are there any good references so I can learn more myself?
Upvotes: 52
Views: 70187
Reputation: 1
RewriteEngine On
# Redirect sites with no extension to .php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule .* %{REQUEST_URI}.php
This is what I came up with. This is based off of the accepted answer and its comments.
Upvotes: 0
Reputation: 156
.htaccess file: add the following lines:
Options +MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
https://alexcican.com/post/how-to-remove-php-html-htm-extensions-with-htaccess/
Now you would have to check for the AllowOverride condition in apache2.log file: set options AllowOverRide All in your web root directory:
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
If you try to access your webpage without the .php extension, you may run into the following error in your apache error.log file: /var/www/html/web/.htaccess: Invalid command 'RewriteEngine', perhaps misspelled or defined by a module not included in the server configuration
To fix this: You don't have the mod_rewrite module installed, To install it: Run the following command: ln -s /etc/apache2/mods-available/rewrite.load /etc/apache2/mods-enabled/rewrite.load
Again, after refreshing the webpage, you might get the folloowing error in error.log file:
[negotiation:error] [pid 4650] [client 127.0.0.1:46461] AH00687: Negotiation: discovered file(s) matching request: /var/www/html/web/test (None could be negotiated). (I was trying to access localhost/web/test.php using the url: localhost/web/test)
To fix it you would have to add the following lines in apache2.conf:
<IfModule mod_mime.c>
AddType application/x-httpd-php .php
AddType application/x-httpd-php .phtml
AddType application/x-httpd-php .php3
AddType application/x-httpd-php .php4
AddType application/x-httpd-php .html
AddType application/x-httpd-php-source .phps
</IfModule>
Now, I can access the test.php file using just the filename: test
Upvotes: 4
Reputation: 52372
RewriteEngine On
RewriteRule something something.php [L]
http://example.com/something
will be handled as if it was a request for something.php
To redirect all requests that are not a physical file to the same name but with .php:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule (.*) $1.php [L]
Upvotes: 76
Reputation: 981
This Option will make clean SEO friendly URL's. Removing.php extensions from the visible URL, and ensuring that URL's accessed without the .php will still display the .php file. I was unable to find another answer that achieved this on multiple threads.
Remember to enable mod rewrite. On Debian or Unbuntu linux the following will work
sudo a2enmod rewrite
and
sudo service apache2 restart
Modify the corresponding section of your apache2.conf file to the following.
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) $1.php [L]
RewriteCond %{THE_REQUEST} /([^.]+)\.php [NC]
RewriteRule ^ /%1 [NC,L,R]
</Directory>
If using .htaccess to re-write ensure that the apache2.conf at least has these options enabled.
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
RewriteEngine On
</Directory>
Finally on some Apache install you may need to also set
AllowOveride All
in a .conf file in /etc/apache2/sites-available or /etc/apache2/sites-enabled.
Rewriting from apache2.conf file as opposed to .htaccess is rumored to be faster.
Upvotes: 1
Reputation: 7836
For directing a few single files it might be easier to avoid the RewriteEngine.
I've used this:
<Files $filename>
SetHandler application/x-httpd-php
</Files>
That's less powerful and flexible than rewrite but probably faster and seems like the way to go if you wish to just have a few specific files handled as PHP.
Upvotes: -1
Reputation: 41249
The accepted answer above does not remove the .php
extension from urls it just allows you to visit .php
files without extension. To remove the .php
extension completely from urls , you can use the following Rules in root/.htaccess
:
RewriteEngine on
#1)externally redirect "/file.php" to "/file"
RewriteCond %{THE_REQUEST} /([^.]+)\.php [NC]
RewriteRule ^ /%1 [NC,L,R]
#2)Internally map "/file" back to "/file.php"
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ /$1.php [NC,L]
Upvotes: 23
Reputation: 8661
For some reason I wasn't able to get any other solution working properly. I put this in an .htaccess
file at the root of my site. Hopefully this will work for you.
RewriteEngine on #only use this once per .htaccess
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
It's important to keep in mind that if your header or URL contains the .php extension it won't be removed. You'll need to remove the extension.
Use this...
header 'location: /login';
rather than this...
header 'location: /login.php';
Upvotes: 2
Reputation: 19918
To get http://example.com/test linking http://example.com/test.php use:
RewriteEngine on
RewriteRule ^([^\.]+)$ /folder/$1.php [NC,L]
Upvotes: -1
Reputation: 263
Probably what you really want is just a way to not have the suffix at all. The best way I've found of doing this is to use the Files directive in .htaccess or in apache configuration files:
<Files myscript>
SetHandler cgi-script
</Files>
This avoids some of the downsides of rewrites, e.g., direct access of the .php file.
Upvotes: 4