A basic rewriting rule inside a .htaccess file looks like that:


RewriteEngine On

RewriteCond %(REQUEST_FILENAME) !-f
RewriteCond %(REQUEST_FILENAME) !-d
RewriteRule ^user/([a-z0-9]+)$ /profile.php?username=$1 [L]

RewriteCond %(REQUEST_FILENAME) !-f
RewriteCond %(REQUEST_FILENAME) !-d
RewriteRule ^page/([a-z0-9]+)$ /display.php?page=$1 [L]

In short, you only need to enable the RewriteEngine once. You can create as many RewriteConditions as you want, but by default they only apply to the first RewriteRule that follows them.

Here are the two rules that I personally have to use all the time:

redirect from subfolder to domain without condition


RewriteEngine on
RewriteRule ^subfolder/(.*)$ /$1 [R=301,L]

redirect from http:// to https://


RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

It might be worth noting that it is stated in the Apache documentation that they recommend to do these kind of redirects in the redirect directive. However, this is only possible if you have access to the main server configuration file. Otherwise its also fine to use the htaccess file.

Leave a comment

Your email address will not be published. Required fields are marked *