apache redirect from non www to www

Using the rewrite engine is a pretty heavyweight way to solve this problem. Here is a simpler solution: <VirtualHost *:80> ServerName example.com Redirect permanent / http://www.example.com/ </VirtualHost> <VirtualHost *:80> ServerName www.example.com # real server configuration </VirtualHost> And then you’ll have another <VirtualHost> section with ServerName www.example.com for your real server configuration. Apache automatically preserves anything … Read more

How to prevent a file from direct URL Access?

Try the following: RewriteEngine on RewriteCond %{HTTP_REFERER} !^http://(www\.)?localhost [NC] RewriteCond %{HTTP_REFERER} !^http://(www\.)?localhost.*$ [NC] RewriteRule \.(gif|jpg)$ – [F] Returns 403, if you access images directly, but allows them to be displayed on site. Note: It is possible that when you open some page with image and then copy that image’s path into the address bar you … Read more

How to debug Apache mod_rewrite

One trick is to turn on the rewrite log. To turn it on, try this line in your Apache HTTP Server main configuration or current virtual host file (not in .htaccess): LogLevel alert rewrite:trace6 Before Apache httpd 2.4 mod_rewrite, such a per-module logging configuration did not exist yet. Instead you could use the following logging … Read more

How to remove .html from URL?

I think some explanation of Jon’s answer would be constructive. The following: RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d checks that if the specified file or directory respectively doesn’t exist, then the rewrite rule proceeds: RewriteRule ^(.*)\.html$ /$1 [L,R=301] But what does that mean? It uses regex (regular expressions). Here is a little something I made … Read more

How to enable mod_rewrite for Apache 2.2

In order to use mod_rewrite you can type the following command in the terminal: sudo a2enmod rewrite Restart apache2 after sudo /etc/init.d/apache2 restart or sudo service apache2 restart or as per new unified System Control Way sudo systemctl restart apache2 Then, if you’d like, you can use the following .htaccess file. <IfModule mod_rewrite.c> RewriteEngine On … Read more