How can I create friendly URLs with .htaccess?

In the document root for http://website.com/ I’d put an htaccess file like this: <IfModule mod_rewrite.c> RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)$ index.php?url=$1 [QSA,L] </IfModule> Then in your PHP script you can manipulate the $_GET[‘url’] variable as you please: $path_components = explode(“https://stackoverflow.com/”, $_GET[‘url’]); $ctrl=$path_components[0]; $id=$path_components[1]; $tab=$path_components[2];

Redirect all to index.php using htaccess

Your rewrite rule looks almost ok. First make sure that your .htaccess file is in your document root (the same place as index.php) or it’ll only affect the sub-folder it’s in (and any sub-folders within that – recursively). Next make a slight change to your rule so it looks something like: RewriteEngine on RewriteCond %{REQUEST_FILENAME} … Read more

htaccess remove index.php from url

The original answer is actually correct, but lacks explanation. I would like to add some explanations and modifications. I suggest reading this short introduction https://httpd.apache.org/docs/2.4/rewrite/intro.html (15mins) and reference these 2 pages while reading. https://httpd.apache.org/docs/2.4/mod/mod_rewrite.html https://httpd.apache.org/docs/2.4/rewrite/flags.html This is the basic rule to hide index.php from the URL. Put this in your root .htaccess file. mod_rewrite must … Read more

How do I disable directory browsing?

Create an .htaccess file containing the following line: Options -Indexes That is one option. Another option is editing your apache configuration file. In order to do so, you first need to open it with the command: vim /etc/httpd/conf/httpd.conf Then find the line: Options Indexes FollowSymLinks Change that line to: Options FollowSymLinks Lastly save and exit … Read more

How to Set AllowOverride all

In case you are on Ubuntu, edit the file /etc/apache2/apache2.conf (here we have an example of /var/www): <Directory /var/www/> Options Indexes FollowSymLinks AllowOverride None Require all granted </Directory> and change it to; <Directory /var/www/> Options Indexes FollowSymLinks AllowOverride All Require all granted </Directory> then, sudo service apache2 restart You may need to also do sudo … Read more

Redirect non-www to www in .htaccess

Change your configuration to this (add a slash): RewriteCond %{HTTP_HOST} ^example.com$ [NC] RewriteRule (.*) http://www.example.com/$1 [R=301,L] Or the solution outlined below (proposed by @absiddiqueLive) will work for any domain: RewriteEngine On RewriteCond %{HTTP_HOST} !^www\. [NC] RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L] If you need to support http and https and preserve the protocol choice try the following: … 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