By jpgklassen
Running Apache 2 on Ubuntu 14.04. rewrite_module is enabled (sudo apachectl -M
).
In /etc/apache2/apache2.conf (the Ubuntu version of httpd.conf) I have the following code block:
<Directory /var/www/>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond /%{REQUEST_FILENAME}.php -f
RewriteRule ^([a-zA-Z0-9_-\s]+)/$ /$1.php
RewriteCond /%{REQUEST_FILENAME}.html -f
RewriteRule ^([a-zA-Z0-9_-\s]+)/$ /$1.html
</IfModule>
<IfModule mod_expires.c>
...
<IfModule mod_headers.c>
...
</IfModule>
</IfModule>
</Directory>
Ran sudo service apache2 restart
.
When I visit a url on my server without the .php file extension, I get a 404! Why isn’t this working?
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Heya,
Your current rules have a few issues which are probably causing them to not work as expected.
Your regular expression includes a trailing slash (/$
), which means it only matches URLs that end with a slash. This may not be what you want.
The %{REQUEST_FILENAME}
variable contains the full filesystem path to the file or script matching the request. You are appending .php
and .html
to this variable, but then you are comparing it to the root directory because of the leading slash.
Here’s how you can rewrite your rules:
<Directory /var/www/>
<IfModule mod_rewrite.c>
RewriteEngine On
# For PHP files
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [L]
# For HTML files
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*)$ $1.html [L]
</IfModule>
<IfModule mod_expires.c>
...
<IfModule mod_headers.c>
...
</IfModule>
</IfModule>
</Directory>
Note:
The [L]
flag causes mod_rewrite to stop processing the rule set, which is useful for handling multiple rewrite conditions.
^(.*)$
will match any character except newline, which should cover all URLs.
%{REQUEST_FILENAME}
is replaced with $1
, which in this context is the actual requested URL.
After making these changes, make sure to restart Apache:
sudo service apache2 restart
Please ensure your server allows .htaccess overrides or the <Directory>
directive in the server configuration for these mod_rewrite rules to take effect.
Also, these rules will enable extensionless URLs, but they do not enforce them, meaning that the URL with the .php or .html extension will still be accessible. If you want to enforce extensionless URLs (i.e., redirect .php or .html URLs to extensionless ones), you’ll need additional rules.
Hi there,
Can anyone help that how to redirect example.com/abc instead of example.com/abc.php? Please let me know as soon as possible.
Thanks
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.