How to correctly write .htaccess for maintenance mode? - .htaccess

I know the basics for writing an .htaccess that handle a maintenance mode page.
I don't want to use a plugin, as I need to replace the whole WP site while in maintenance mode.
I've tried to add additional rules to allows access to WP Back-End, only for specific IPs.
I've tried several attempts and eventually got very close to what I need.
However, I also want the white-listed IP(s) be able to see the front-end site, without being redirected to the maintenance page, but I'm stuck there.
Here's my current .htaccess:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/images [OR]
RewriteCond %{REQUEST_URI} ^/wp-admin/.* [OR]
RewriteCond %{REQUEST_URI} ^/wp-content/.* [OR]
RewriteRule .+ - [L]
RewriteCond %{REMOTE_ADDR} !^93\.62\.93\.79
RewriteCond %{REQUEST_URI} !^/maintenance-mode\.html$ [NC]
RewriteCond %{REQUEST_URI} !\.(jpe?g?|png|gif) [NC]
RewriteRule .* /maintenance-mode.html [R=302,L]
What I'm supposed to add/change?

Did you found a solution? I don't tested it with WP but this worked for me for another CMS.
Options +FollowSymLinks
RewriteEngine On
# exclude file (otherwise infinity loop)
RewriteCond %{REQUEST_URI} !^/under-construction/maintenance\.php$
# exclude images and css
RewriteCond %{REQUEST_URI} !\.(jpe?g?|png|gif|css) [NC]
# If it's not your IP address
RewriteCond %{REMOTE_HOST} !^111\.222\.333\.444$
# Redirect /abc to example.com/abc.
RewriteRule ^(.*)$ http://www.domain.com/under-construction/maintenance.php [R=307,L]
Perhaps it will help you.

Related

Use .htaccess to block PhantomJS bot

I want to block traffic from something that appears to be a bot or some other malicious traffic (haven't quite figured out what it is, but I suppose I don't want it). So far I have been blocking it by IP, however the traffic keeps coming from different locations.
Common for this traffic is that the user agent is PhantomJS, and they all show the following local address (perhaps with some variations):
file:///home/poo_master/price_parse/resource_cache/140578757867264tmp2.html
Is it possible to use .htaccess to block either PhantomJS or anything containing "poo_master/price_parse/resource_cache/"
To block bots/scrapers by useragent OR by requested URL add these rewrite rules to .htaccess:
RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} ^.*(PhantomJS|wget|HTTrack|python).*$ [OR]
RewriteCond %{REQUEST_URI} ^.*poo_master/price_parse.*$
RewriteRule . - [F,L]
Update your .htaccess file with below code. Hope it will work.
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_USER_AGENT} ^.*(PhantomJS|wget|HTTrack|python).*$ [OR]
RewriteCond %{REQUEST_URI} ^.*poo_master/price_parse.*$
RewriteRule ^(.*)$ index.php/$1 [L,QSA]

Adding options to existing .htaccess

I have this .htaccess code which I have put together from different sources and I am having trouble adding a couple of extra options to it.
<Limit GET POST PUT>
order deny,allow
deny from all
allow from XXX.XXX.XXX.XXX
</Limit>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f [OR]
RewriteCond %{REQUEST_FILENAME} !-d
# allow things that are certainly necessary
RewriteCond %{REQUEST_URI} "/css/" [OR]
RewriteCond %{REQUEST_URI} "/js/" [OR]
RewriteCond %{REQUEST_URI} "/images/"
RewriteRule .* - [L]
RewriteRule ^([a-zA-Z0-9-/_]+)/([a-zA-Z0-9-/_%]+)/([a-zA-Z0-9-/_]+)/([a-zA-Z0-9-/_]+)$ index.php?primary=$1&secondary=$2&tertiary=$3&quaternary=$4 [QSA]
RewriteRule ^([a-zA-Z0-9-/_]+)/([a-zA-Z0-9-/_%]+)/([a-zA-Z0-9-/_]+)$ index.php?primary=$1&secondary=$2&tertiary=$3 [QSA]
RewriteRule ^([a-zA-Z0-9-/_]+)/([a-zA-Z0-9-/_%]+)$ index.php?primary=$1&secondary=$2 [QSA]
RewriteRule ^([a-zA-Z0-9-/_]+)$ index.php?primary=$1
Firstly I would like to add the forced use of www. in the URL. When I have tried to add this, the resulting URL becomes the output of the current RewriteRules, containing the GET variables. I believe this is because the rules work on a loop?
Another thing I would like to do is catch anything that doesnt meet the criteria of the current rules and send that to something like index.php?primary=error. The way it works now is almost perfect for my use because if there is a malformed URL or illegal character, the site is not going to even attempt to display the page. The site will create all URLs safely, so any bad URLs would be the result of experimenting in the address bar, but it would be nice to have an error page rather than a page not found.
Thanks in advance.. And sorry for the muddle-through use of RewriteRule!
Your regex are incorrect since hyphen can be unescaped only when it appears as first or last symbol in a character class, it needs to be escaped otherwise.
So instead of: [a-zA-Z0-9-/_%]
Use: [\w%/-]
Your complete set of rules:
RewriteEngine On
RewriteBase /
# add www in the URL
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,NE,L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d [OR]
# allow things that are certainly necessary
RewriteCond %{REQUEST_URI} ^/(css|js|images)/ [NC]
RewriteRule ^ - [L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ index.php?primary=$1&secondary=$2&tertiary=$3&quaternary=$4 [QSA,L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ index.php?primary=$1&secondary=$2&tertiary=$3 [QSA,L]
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?primary=$1&secondary=$2 [QSA,L]
RewriteRule ^([^/]+)/?$ index.php?primary=$1 [QSA,L]

.htaccess url rewriting - exceptions for certain files only

This is my .htaccess:
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !index\.php [NC]
RewriteCond %{REQUEST_FILENAME} !\.php$ [NC]
RewriteRule ^([^/]+)/?$ index.php?page=$1 [QSA]
Basically if you enter /test, it will work perfectly, and if you go to /test.php, it will show the PHP page without the rewrite rule, which is what I need for certain pages only.
let's say there are some pages (a few) where I would want this to be able (to load the whole .php page), but ALL the other pages, what I want is that if someone goes to /contact.php, it redirects to /contact, to the right one.
So let's say for "result.php" and "search.php", I want it to be able to load the whole PHP page, but for the rest, I want it to redirect to the right adress (so /contact.php => /contact).
How could I do it? Basically I need to add this RewriteRule ( RewriteCond %{REQUEST_FILENAME} !\.php$ [NC]) only for a few pages (that I would determine myself), and for the rest of the pages I need another RewriteCond (that redirects all the "test.php" to "test")
Thanks a lot!
You can put all those exception in your first RewriteCond and alter 2nd RewriteCond to skip real files and real directories like this:
RewriteCond %{REQUEST_URI} !/(index|result|search)\.php [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ /index.php?page=$1 [QSA,L]

Multiple addon domains, one htaccess file

Lets say, my root domain name is main.com and I have two addon domains: addon1.com and addon2.com.
My script is already ready and I can see my websites like this:
www.main.com/show.php?domain=addon1.com
But, what I want is displaying websites through their domains. I mean when I open addon1.com, I want to see output of show.php?domain=addon1.com. Also these two domains are added as addon domain and their directory are:
main.com/addon1.com/
main.com/addon2.com/
I wrote a htaccess file to root folder (main.com/.htaccess)
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.addon1\.com$ [NC]
RewriteRule ^(.*)$ /show.php?domain=addon1.com&$1
RewriteCond %{HTTP_HOST} ^www\.addon2\.com$ [NC]
RewriteRule ^(.*)$ /show.php?domain=addon2.com&$1
But I'm getting 500 interval error. Any advice?
Thanks in advance.
Your rules are looping. The /show.php is going back through the rewrite engine and looping indefinitely. You need to add conditions so they don't loop:
RewriteCond %{HTTP_HOST} ^www\.addon1\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/show.php
RewriteRule ^(.*)$ /show.php?domain=addon1.com&$1
RewriteCond %{HTTP_HOST} ^www\.addon2\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/show.php
RewriteRule ^(.*)$ /show.php?domain=addon2.com&$1
You needed to include "RewriteBase" to help out.
# tell mod_rewrite to activate and give base for relative paths
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
# for the active site in hosting root folder,
# tell it not to look for a subfolder by skipping next rule
RewriteCond %{HTTP_HOST} ^(www\.)?main\.com [NC]
RewriteRule ^(.*)$ - [S=1]
# the domain-name = sub-folder automation
# thus addon-domain.com in /addon-domain(\.com)?/
RewriteCond %{HTTP_HOST} ([^.]+)\.com
RewriteCond %{REQUEST_URI} !^/%1
RewriteRule ^(.*)$ %1/$1 [L]
# to answer your question swap the above
# domain-name = sub-folder automation for this rule
RewriteCond %{HTTP_HOST} ([^.]+)\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/show.php
RewriteRule ^(.*)$ /show.php?domain=%1&$1 [L]
# although, the above line may require this instead
RewriteRule . main.com/show.php?domain=%1&$1 [L]

.htaccess mod_rewrite redirection between domains

I have two domains, cshen.ca and cshen.net, both point to the same place.
I'm trying to accomplish the following with mod_rewrite:
cshen.net is 301 redirected to cshen.ca
www.cshen.net or www.cshen.ca are both redirected to cshen.ca
the path after the domain is preserved after being redirected, for example www.cshen.net/foo/bar/ would be redirected to cshen.ca/foo/bar/
I've tried a variety of rules, but can't seem to get it to work.
RewriteCond %{HTTP_HOST} ^cshen\.net$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://cshen.ca/$1 [R=301,L]
This accomplishes the first two rules, but redirects everything back to the home page, and does not preserve the rest of the URL.
RewriteCond %{HTTP_HOST} ^cshen\.net$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^/(.*)$ http://cshen.ca/$1 [R=301,L]
Making a small change and adding a slash '/' to the RewriteRule preserves the rest of the URL, but only www.cshen.ca is being redirected. Neither cshen.net nor www.cshen.net are being redirected anywhere.
I've also tried Apache's guide and used this code:
RewriteCond %{HTTP_HOST} !^cshen\.ca [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^/(.*) http://cshen.ca/$1 [L,R]
I thought this would work, since it should redirect any url that isn't cshen.ca, however, like the second piece of code, it does nothing to cshen.net or www.cshen.net.
I've just about ran out of ideas of other things to try. I would appreciate it if someone could help out!
Addendum: in case it matters, I'm using WordPress's pretty URLs, and the rewrite rules for that are:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
I've tried putting my rules before and after the WordPress rules (when I put it before I'd of course add RewriteEngine On before my rules), doesn't make any difference.
Thanks!
don't know how to edit, solved by placing the code from apache before WP's
must have forgotten to test this earlier.

Resources