don't rewrite static css/js/img files - .htaccess

I'm trying to get my htaccess file not to rewrite my static files (js/css/images).
This is my current htaccess file:
RewriteEngine on
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule !\.(jpg|css|js|gif|png)$ public/ [L]
RewriteRule !\.(jpg|css|js|gif|png)$ public/index.php?url=$1
How do I rewrite it?

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^.*\.(jpg|css|js|gif|png)$ [NC]
RewriteRule ^(.*)$ public/index.php?url=$1
All requests to not existing files which doesn't end with listed extensions (case nonsensitive match) are rewritten to public/index.php passing the current URL as url= GET argument

This only works for the one line below it. So if you have many RewriteRules it could help to use something like this:
RewriteEngine on
RewriteRule ^(.*?)\.(php|css|js|jpg|jpeg|png|pdf)$ - [L]
RewriteRule ^(.+)/(.+)/?$ index.php?page=$1&subpage=$2 [L]
RewriteRule ^(.+)$ index.php?page=$1 [L]

for posterity.
Maybe my rules will be useful
\www\.htaccess
#file: \www\.htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} \.(css|js|html?|png|gif|jpg|jpe?g|svg|xls.?|pdf|docx?|eot|ttf|woff2?)$ [NC]
RewriteRule (.*\.[css|js|html?|png|gif|jpg|jpe?g|svg|xls.?|pdf|docx?|eot|ttf|woff2?]+) public/$1?a [QSA,L]
RewriteCond %{REQUEST_URI} \.(css|js|html?|png|gif|jpg|jpe?g|svg|xls.?|pdf|docx?|eot|ttf|woff2?) [NC]
RewriteRule (.*\.[css|js|html?|png|gif|jpg|jpe?g|svg|xls.?|pdf|docx?|eot|ttf|woff2?]+) public/$1 [QSA,L]
#RUN TEST
RewriteCond %{REQUEST_URI} !\.(css|js|html?|png|gif|jpe?g|svg|xls.?|pdf|docx?|eot|ttf|woff2?)$ [NC]
RewriteRule ^(.*)$ index.php?REQUEST_FILENAME=%{REQUEST_FILENAME}&date=$1&REQUEST_URI=%{REQUEST_URI} [QSA,L]
#END TEST
RewriteRule . index.php?url=%{REQUEST_URI} [QSA,L]
ErrorDocument 404 index.php?error=404
\www\index.php
<?php
// file: /www/index.php
var_dump($_GET);
\www\app\.htaccess
#file: \www\app\.htaccess`
RewriteEngine On
RewriteRule ^(.*)$ ../_%{REQUEST_URI} [QSA,L]
\www\public\.htaccess
#file: \www\public\.htacces`
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=%{REQUEST_URI}&rewrite=$1 [QSA,L]
/www/public/index.php
<?php
// file: /www/public/index.php
var_dump($_GET, __DIR__);

Related

htaccess multiple rules - paths break

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^user/([0-9]+)/?$ index.php?a=user&id=$1 [QSA,L]
RewriteRule ^img/([0-9]+)/?$ index.php?a=img&id=$1 [QSA,L]
RewriteRule ^(.*)$ index.php?a=$1 [QSA,L]
Options -Indexes
The htaccess rules are breaking the paths of the files.
Someone have an idea?
This should work :
Options -Indexes
RewriteEngine On
RewriteRule ^user/([0-9]+)/?$ index.php?a=user&id=$1 [QSA,L]
RewriteRule ^img/([0-9]+)/?$ index.php?a=img&id=$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?a=$1 [QSA,L]

remove index.php and foldername with mod_rewrite

The URL Structure on my website is the following: http://www.mydomain.tld/index.php/pages/sitename. Now I want to remove the index.php/pages, to let the URL Structure be like http://www.mydomain.tld/sitename. Furthermore, I want to rename some sitename's individual.
I already removed the index.php, thats no problem. So currently my .htacces-File looks like
DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|(.*)\.swf|forums|images|css|downloads|jquery|js|robots\.txt|favicon\.ico|pam)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php?$1 [QSA,L]
RewriteCond %{HTTP_HOST} ^mydomain.tld\.de$ [NC]
RewriteRule ^(.*)$ http://www.mydomain.tld/$1 [R=301,L]
But if I append /pages to the first RewriteCond or RewriteRule, it doesn't work.
Do you have some ideas?
Have your .htaccess like this:
DirectoryIndex index.php
RewriteEngine on
RewriteCond %{HTTP_HOST} ^mydomain.tld\.de$ [NC]
RewriteRule ^(.*)$ http://www.mydomain.tld/$1 [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^((?!index\.php/pages/).*)$ index.php/pages/$1 [NC,L]

htaccess for restful apis with sub directories not working

I have a directory structure for my apis as:
api/
cameras/
index.php
...
.htaccess
bar/
set/
index.php
scan/
index.php
retrieve/
index.php
list/
index.php
Before I added the bar folder I had this working with my .htaccess folder
RewriteEngine On
RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule ^(.*) - [E=HTTP_AUTHORIZATION:%1]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php [L]
I then added this to make it work.
RewriteEngine On
RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule ^(.*) - [E=HTTP_AUTHORIZATION:%1]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule bar/set/(.*) bar/set/index.php [L]
RewriteRule bar/scan/(.*) bar/scan/index.php [L]
RewriteRule bar/list/(.*) bar/list/index.php [L]
RewriteRule bar/retrieve/(.*) bar/retrieve/index.php [L]
RewriteRule (.*) index.php [L]
They all work except the first one in the list set. What have I done wrong?
I think the order of your conditions might be causing an issue. Without seeing the URL's you are using I would have to assume. However RewriteCond only applies to the rule directly following it. You put the new rules between the conditions and your original rule. Change the order and try it this way and see how it works.
RewriteEngine On
RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule ^(.*) - [E=HTTP_AUTHORIZATION:%1]
RewriteRule ^bar/set/(.*)$ bar/set/index.php [L]
RewriteRule ^bar/scan/(.*)$ bar/scan/index.php [L]
RewriteRule ^bar/list/(.*)$ bar/list/index.php [L]
RewriteRule ^bar/retrieve/(.*)$ bar/retrieve/index.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) index.php [L]
RewriteCond only gets applied to next RewriteRule. Try this code instead:
DirectoryIndex index.php
RewriteEngine On
RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule ^ - [E=HTTP_AUTHORIZATION:%1]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^(bar)/(scan|set|scan|retrieve)/(.*)$ $1/$2/index.php [L,NC]
RewriteRule . index.php [L]

remove index.php from url only for the client

I know there are some questions like this out there but the solutions are not helpful for me.
I´m using htaccess to redirect to the index.php if the request file isn´t defined (code above).
I´d like to add a code-line that if the user requests for example:
http://www.example.com/test/index.php
he will redirect to
http://www.example.com/test/
but internal it will redirect to the index.php.
Is there a way to extend my code so that it works like I want it to?
# Add Slash
RewriteCond %{REQUEST_URI} !(/$|\.)
RewriteRule (.*) %{REQUEST_URI}/ [R=301,L]
# Redirect to index.php if the following sites were not match
RewriteBase /
RewriteCond %{REQUEST_URI} \.(php|sql|tgz|gz|txt|ttf|TTF|log|txt|ini|html|xml|xhtml|htm)$
RewriteCond %{REQUEST_URI} !^/index.php$
RewriteCond %{REQUEST_URI} !^/phpinfo.php$
RewriteCond %{REQUEST_URI} !^/out.php$
RewriteCond %{REQUEST_URI} !^/sitemap[^0-9]*\.xml$
RewriteCond %{REQUEST_URI} !^/robots.txt$
RewriteRule ^(.*)$ /index.php?dir=$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/$
RewriteRule ^(.*)$ /index.php?dir=$1 [QSA,L]
Complete code:
DirectoryIndex index.php index.html
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
# Take off index.php
RewriteCond %{REQUEST_URI} ^(.*/)index\.php$ [NC]
RewriteRule ^ http://%{HTTP_HOST}%1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} \.(php|sql|tgz|gz|txt|ttf|TTF|log|txt|ini|html|xml|xhtml|htm)$
RewriteCond %{REQUEST_URI} !^/((index|phpinfo|out)\.php|robots\.txt|sitemap[^0-9]*\.xml)$
RewriteRule ^(.*)$ /index.php?dir=$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ /index.php?dir=$1 [QSA,L]
# Add Slash for non-files
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule [^/]$ %{REQUEST_URI}/ [R=301,L]

.htaccess how to use RedirectMatch

Since I changed the structure of my website, some of the links became broken.
What rule should I write in .htaccess to fix the problem.
before it was like this
/news/194-kozyrnyj-vecher-premier-ligi.html
and now it looks like this
/news/2-news/194-kozyrnyj-vecher-premier-ligi.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/index.php
RewriteCond %{REQUEST_URI} (/|\.php|\.html|\.htm|\.feed|\.pdf|\.raw|/[^.]*)$ [NC]
RewriteRule (.*) index.php
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L]
RewriteCond %{REQUEST_URI} !^/news/2-news/(.*)
RewriteRule ^news/(.*)$ /news/2-news/$1 [R=permanent,L]
should be:
RewriteCond %{REQUEST_URI} !^/news/2-news/(.*)
RewriteRule ^news/(.*)$ /news/2-news/$1 [R=permanent,L]
in your .htaccess file

Resources