Apache mod_rewrite rule
Let's see what the following lines mean:
RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*) index.php
The RewriteRule ^(.*) index.php will redirect everything entered from "http://www.my-domain.com" to index.php so that we can parse the URI.
The two lines above it:
RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d
means that if the file with the specified name in the browser doesn't exist, or the directory in the browser doesn't exist, then proceeds to the rewrite rule below the two lines:
RewriteRule ^(.*) index.php
Therefore, index.php will be used if a file or a directory is not available.
To enforce a no-trailing-slash policy:
RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)/$ /$1 [L,R=301]
To enforce a trailing-slash policy:
RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*[^/])$ /$1/ [L,R=301]
When permanently moving a web site, or a web page, best practice is to use a 301 redirect. 302 means a temporary redirect, and it's the default.
Here is a sample of redirecting http to https for a subdomain:
<VirtualHost *:443> ServerAdmin webmaster@localhost ServerName subdomain.mydomain.com DocumentRoot /var/www/home/myroot SSLEngine on SSLCertificateFile /etc/apache2/ssl-cert/mydomain.crt SSLCertificateKeyFile /etc/apache2/ssl-cert/mydomain.key SSLCertificateChainFile /etc/apache2/ssl-cert/gd_bundle.crt ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/ <Directory "/usr/lib/cgi-bin"> AllowOverride None Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch Order allow,deny Allow from all </Directory> ErrorLog ${APACHE_LOG_DIR}/error.log # Possible values include: debug, info, notice, warn, error, crit, # alert, emerg. LogLevel warn CustomLog ${APACHE_LOG_DIR}/access.log combined Alias /doc/ "/usr/share/doc/" <Directory "/usr/share/doc/"> Options Indexes MultiViews FollowSymLinks AllowOverride None Order deny,allow Deny from all Allow from 127.0.0.0/255.0.0.0 ::1/128 </Directory> </VirtualHost> <VirtualHost *:80> ServerName subdomain.mydomain.com Redirect "/" "https://subdomain.mydomain.com" </VirtualHost>
Note we need to add ServerName and VirtualHost block with 80.
Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization