Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Please, explain for total beginner this .htaccess file

Tags:

php

.htaccess

I have some troubles understanding how to dynamically put together simple one language website. I would really appreciate if somebody could explain me in baby language what every part of the code below means:

RewriteEngine On

RewriteCond %{REQUEST_URI} !\.(php|css|js|gif|png|jpe?g|pdf)$

RewriteRule (.*)$ templates/index.php [L]

Thank you in advance!

like image 340
Kristine Avatar asked Dec 25 '22 18:12

Kristine


2 Answers

# Enable RewriteEngine to rewrite URL patterns
RewriteEngine On

# Every URI that not (! operator) ends with one of .php, .css, .js, .gif, .png, .jpg, .jpeg or .pdf
RewriteCond %{REQUEST_URI} !\.(php|css|js|gif|png|jpe?g|pdf)$

# Will be redirected to templates/index.php
RewriteRule (.*)$ templates/index.php [L]

# Sample
# /foo/bar.php -> /foo/bar.php
# /foo/bar.html -> templates/index.php
like image 190
TiMESPLiNTER Avatar answered Dec 28 '22 09:12

TiMESPLiNTER


RewriteEngine On

Turns the rewrite engine on

RewriteCond %{REQUEST_URI} !\.(php|css|js|gif|png|jpe?g|pdf)$

Matches all requests which end not with .php, .css etc.

  • ! = negates the following expression ("matches not")
  • \. = A single point (has to be escaped so it's taken literally. Without the backslash it would match every character)
  • (php|css|js|gif|png|jpe?g|pdf) = One of these options. jpe?g means the e is optional, so it matches jpg and jpeg
  • $ = the end of the request.

RewriteRule (.*)$ templates/index.php [L]

Redirects all request not matching the regular expression to templates/index.php. [L] means it's the last rule, so not other rules from this .htaccess get applied.

like image 21
Reeno Avatar answered Dec 28 '22 11:12

Reeno