redirect to maintenance page issue - regex

My maintenance page to my site has a rather odd glitch, the page is redirecting but something in the htaccess code is blocking the css and javascript files. Please take a look at my code there's something in it that's not quite right, because5 when I remove the redirect the css loads as normal.
#
# Redirection to the Maintenance Page
RewriteCond %{REMOTE_ADDR} !^25\.210\.157\.0
RewriteCond %{REQUEST_URI} !/maintenance.html$
RewriteCond %{REQUEST_URI} !\.(jpg|jpeg|png|gif)
RewriteRule .* /maintenance.html [R=307,L]
#
#
Found the answer, but is there any easier way, instead of selecting all type of file to an all encompassing command?
RewriteCond %{REQUEST_URI} !\.(jpg|jpeg|png|gif|css|js|ico|woff|eot|svg|ttf)

You can use:
RewriteCond %{REMOTE_ADDR} !^25\.210\.157\.0
RewriteCond %{REQUEST_URI} !/maintenance\.html$ [NC]
# If the request is not for a valid directory
RewriteCond %{REQUEST_FILENAME} !-d
# If the request is not for a valid file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* /maintenance.html [R=307,L]

Related

.htaccess redirect URLs with specific path to 404 and exclude some URLs with the same path as exception

I have an application on Codeigniter(3.1.11). Basically, I want a 404 redirect of URLs which have dashboard in URI. Like these:
dashboard/foo/bar
dashboard/foo-2/bar-2
dashboard/something
...
Also, I want to keep some exceptions in redirect rule so, some of the specific URLs which have dashboard as path URI should be excluded from this redirect. Let's say I want to exclude some URLs like:
dashboard/new-foo/new-bar
dashboard/one-thing/abc
dashboard/another-thing/xyz
I have given a few tries but the exclude rule is not working. It redirects all URLs to 404. This is what I have in my .htaccess:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/(dashboard/new-foo/new-bar) [NC] # Exclude this url (this condition is not working)
RewriteRule ^(.*)$ dashboard/$1 [R=404,L]
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
It can be done in a single rule with negative lookahead conditions like this:
RewriteEngine on
RewriteRule ^dashboard(?!/new-foo/new-bar|/one-thing/abc|/another-thing/xyz) - [R=404,NC,L]
RewriteCond $1 !^(index\.php|resources|robots\.txt) [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
I'm not expert with htaccess but I am pretty sure you need 2 RewriteCond lines, one to include all the dashboard urls and then the second to exclude the new ones. Each RewriteCond is implicitly joined with an "AND" so if you have many different patterns to exclude and you need a 3rd RewriteCode then you will need to join the 3rd with an "OR" on the 2nd condition.
e.g.
RewriteCond %{REQUEST_URI} ^/dashboard/(.*) [NC]
RewriteCond %{REQUEST_URI} !^/dashboard/new-(.*) [NC]
RewriteRule ^(.*)$ 404-page/$1 [R=404,L]
There are a couple of other things I'd like to mention: 1) Your RewriteRule redirects back to a /dashboard URL so you could possibly end up in a continuous look here. 2) You don't need to turn on Rewrite engine twice, once at the top is enough.
If the rewrite rules in htaccess are getting complicated then perhaps you could use your index.php file to handle it (or some other method within Codeigniter).

Unexpected behavior with custom .htaccess rewrite rules

So I have been working on some rewrite rules which aren't working as expected. These are some sample requests I am trying to rewrite:
/ -> /index.php?page=Home
/Home -> /index.php?page=Home
/Teaching/Foo -> /index.php?page=Teaching&id=Foo
/Teaching/Bar -> /index.php?page=Teaching&id=Bar
/Download/ue8 -> /index.php?action=Download&id=ue8
/Download/24a -> /index.php?action=Download&id=24a
(default) -> /index.php?page=Home
** OR ALTERNATIVELY **
(default) -> /index.php?page=FileNotFound
(and maybe rewrite the visible URL to /FileNotFound)
I mainly want to hide as much of the urls as possible and prevent both directory listing and direct access to my files located in specific folders and only allow access to downloadable files via /Download/FileId while having my usual pages for different lectures accesible via /Teaching/SomeLecture.
So far I have been using this snippet for the /Home and /Teaching stuff:
RewriteEngine On
RewriteBase /
# Redirect Trailing Slashes
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^Teaching/([A-Za-z0-9]*)$ index.php?page=Teaching&id=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^Home$ index.php?page=Home [L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index\.php [NC]
RewriteRule ^ %1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [L,R=301]
I am not completely sure about all of these directives and I noticed, that there are currently some flaws to it.
Trying to access a non-existant file, e.g. /Files/Bad/Path.pdf, forwards the user to /?page=Home which should be redirected either to /, /Home or display the contents of /index.php?page=FileNotFound without changing the URL at all or redirecting to /FileNotFound depending on the rule of (default). I am not really sure which solution might be the most suitable in this scenario.
Trying to access some folders which do exist results in an infinite redirection loop while folders which do not exist apparently redirect to /. In both cases it feels right to redirect to /FileNotFound I suppose?
Could you please work out a set of rules that might suit my needs in this case?
You have many redundant directives in your .htaccess. Replace all of your .htaccess with this:
# Turn off mod_spelling
<IfModule mod_speling.c>
CheckSpelling off
CheckCaseOnly off
</IfModule>
Options -MultiViews
RewriteEngine On
RewriteBase /
# block direct access to file and directories in these directories
RewriteCond %{REQUEST_URI} !\.(?:jpe?g|gif|bmp|png|tiff|css|js)$ [NC]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(Templates|Files) - [NC,F]
# remove index.php
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(.*?)index\.php$ /$1 [L,R=301,NC,NE]
# Redirect Trailing Slashes
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(Download|Teaching)/([\w-]+)/?$ index.php?page=$1&id=$2 [L,QSA,NC]
RewriteRule ^(Home)?/?$ index.php?page=Home [L,NC,QSA]
Make sure to clear your browser cache before testing this.

Rewrite urls to not include file extensions but allow subdomains to act normally

I need to
Remove the file extension from all of the urls on my site (mywebsite.com/about.html >> mywebsite.com/about/)
Always add a trailing / to the end of the url (mywebsite.com/about >> mywebsite.com/about/)
Allow for an exception that one of the nav items links to a pdf, not an html document (mywebsite.com/calendar.pdf >> mywebsite.com/calendar/
Allow subdomains to go to their folder instead of being rewritten (My folder tree is public_html>my main site files and a dev folder>(inside dev folder) index.html) I need the subdomain to link to that dev folder, currently the url rewriting changes dev.mywebsite.com to public_html/dev.html
This is my current, hacked together htaccess file:
RewriteEngine On
RewriteRule ^calendar/?$ files/2013-2014_calendar.pdf
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ $1.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
Sorry, this might be a duplicate or semi-duplicate. I looked around as much as I could but couldn't find anything that applied to my situation exactly or that I understood. Thanks guys!
Try this code:
RewriteEngine On
RewriteBase /
# skip dev. subdomain
RewriteCond %{HTTP_HOST} ^dev\. [NC]
RewriteRule ^ - [L]
# calendar rule
RewriteRule ^calendar/?$ files/2013-2014_calendar.pdf [L]
# hide .html rules
RewriteCond %{THE_REQUEST} \s/+(.+?)\.html[\s?] [NC]
RewriteRule ^ %1/ [R=301,L,NE]
# add a trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule [^/]$ %{REQUEST_URI}/ [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.html -f
RewriteRule ^(.+?)/?$ $1.html [L]

Rewrite rule with 2 variables with "/" separation

I have an .htaccess file located in a folder "/mixtapes/" I am trying to get the URL mydomain.com/music/downloads/mixtapes/this-title/id to execute mydomain.com/music/downloads/mixtapes/item.php?title=variable1&id=variable2
I currently have the below way somewhat working but it only uses the id and I need both variables (../mixtapes/title/id)separated by "/" and for some reason with the below code the index page inside "/mixtapes/" does not work.I am stumped! I am somewhat new to this and any help is greatly appreciated!
BTW on my index page the passing url to item.php page is rewritten to <a href="title/id">I just cant seem to get it to properly execute item.php?title=a&id=b with the format mixtapes/title/id
Current htaccess file located in "/mixtapes/"
# turn mod_rewrite engine on
RewriteEngine On
# rewrite all physical existing file or folder
RewriteCond %{REQUEST_FILENAME} !-f [OR]
RewriteCond %{REQUEST_FILENAME} !-d
# allow things that are certainly necessary
RewriteCond %{REQUEST_URI} "/css/" [OR]
RewriteCond %{REQUEST_URI} "/images/" [OR]
RewriteCond %{REQUEST_URI} "/images/" [OR]
RewriteCond %{REQUEST_URI} "/javascript/"
# rewrite rules
RewriteRule ^mixtapes/item.php(.*) - [L]
RewriteRule ^(.*) item.php?id=$1 [QSA]
The comments in your .htaccess actually state wrong
# turn mod_rewrite engine on
RewriteEngine On
# if requested URL is NOT a existing file
RewriteCond %{REQUEST_FILENAME} !-f [OR]
# or if requested URL is NOT a directory
RewriteCond %{REQUEST_FILENAME} !-d
# CSS, images, JavaScript are files and we will never pass this point when those are requested, next rules can be skipped
# rewrite rules
# rewrite /mixtapes/title/id to item.php?title=title&id=id
Rewrite ^mixtapes/([^/]+)/([0-9]+) item.php?title=$1&id=$2 [L]
# catch all other requests and handle them ( optional, default would be 404 if not a physically existing file )
Rewrite (.*) index.php [L,QSA]
I've assumed that your id is a numeric value.
Be aware with the use of the title in php. Don't output this directly but you can use it to verify your URL and redirect wrong title/id combos

Removing index.php from url using .htaccess even if the user requests it

I never want index.php to show up in my URL, even if the user inputs it. Is this possible?
This is variation whatever after several tries. I've come close a few times but this is where it's at for now.
RewriteEngine On
RewriteBase /sub-dir/
RewriteCond %{REQUEST_URI} index\.php$ //If URL ends in index.php
RewriteRule (.*)index\.php $1 //Somehow remove index.php from the url
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
Currently I have permalink set up where if the user enters domain.com/sub-dir/my-perma-lin/ it generates a string on the page based on my-perma-link to look like My Perma Link. What I'd like is if the user submits any URL ending in index.php it just removes that from the URL but leaves everything else as is.
domain.com/sub-dir/index.php --> domain.com/sub-dir/
domain.com/sub-dir/my-perma-link/index.php --> domain.com/sub-dir/my-perma-link
I've written quite a few rules in http://htaccess.madewithlove.be/ that work perfectly but when I upload it (to Dreamhost) nothing works.
This for example should work according to the the tester
RewriteEngine On
RewriteBase /sub-dir/
RewriteCond %{REQUEST_URI} \.php //Not needed but thought it would/should help
RewriteRule (.*)(index\.php)+ $1 [L,R=301,NC]
But it just removes everything after /sub-dir/
I'm either missing something super obvious or it's not possible ...
You need to add some flags to your rule:
RewriteEngine On
RewriteBase /sub-dir/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.*)index\.php(\?|\ )
RewriteRule ^ /%1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
You can ditch the RewriteCond %{REQUEST_URI} index\.php$ condition, as that's being checked by the regex in the RewriteRule. You need to include a $ at the end of the regex, and include the flags L to stop rewriting and R=301 to redirect.