htaccess code for maintenance page redirect - apache

I set up a maintenance page that I could enable through an htaccess file. The html file is located in a folder called "maintenance".
The html file has some images in it. However, visitors to the page see no images, even though I added a RewriteCond line to (theoretically) allow them.
If I try to visit the URL of an image file in the browser directly, it redirects to the maintenance.htm page. I do not want image files to be redirected.
Am I missing something?
#RewriteCond %{REMOTE_ADDR} !^127.0.0.1$
#RewriteCond %{REMOTE_ADDR} !^111.111.111.111$
RewriteCond %{REQUEST_URI} !/maintenance/maintenance\.htm$ [NC]
RewriteCond %{REQUEST_URI} !\.(jpg|jpeg|png|gif|css|ico)$ [NC]
RewriteRule ^(.*)$ /maintenance/maintenance.htm [R=302,L]

I think this will work:
<IfModule mod_rewrite.c>
RewriteEngine on
# RewriteCond %{REMOTE_ADDR} !^127.0.0.1$
# RewriteCond %{REMOTE_ADDR} !^111.111.111.111$
RewriteCond %{REQUEST_URI} !/maintenance/maintenance\.htm$
RewriteCond %{REQUEST_URI} !\.(jpg|jpeg|png|gif|css|ico)$
RewriteRule ^(.*)$ /maintenance/maintenance.htm [L]
</IfModule>
I added a condition to confirm the rewrite module is active before procesing the rule. The question rule and conditions are not modified.
This rule was tested here.

Related

htaccess remove folder redirect

I have a problem removing folders from an url. I want that google / old links aren't broken. The old webpage had several sections with a structure like this
example.com/news/items/entry1.html
example.com/news/items/entry2.html
example.com/blog/items/foo.html
The new page has the urls like this:
example.com/news/entry1
example.com/news/entry2
example.com/blog/foo
Removing html was rather straight forward
<IfModule mod_rewrite.c>
RewriteEngine On
# Send would-be 404 requests to Craft
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/(favicon\.ico|apple-touch-icon.*\.png)$ [NC]
RewriteRule (.+) index.php [QSA,L]
RewriteCond %{THE_REQUEST} /([^.]+)\.html[\s?] [NC]
RewriteRule ^ /%1 [R=302,L,NE]
</IfModule>
The part I'm struggling with is removing the 'items' part. The rules I found only worked for request path like 'example.com/items/subfolder1/...'
Any help would be greatly appreciated.
To remove /items/ from your URLs you can use the following in your .htaccess file:
RewriteEngine On
RewriteRule ^(.*)/items /$1 [L,R=301]
So for example, this will take the URL: http://example.com/news/items/entry1 and turn it into http://example.com/news/entry1
Make sure you clear your cache before testing this.

htaccess redirect in joomla WITHOUT landing on index.php

I've added an redirect from an old domain to my new domain in the .htaccess file of my Joomla 3.x site, running on Apache.
This is the code I'm trying to get working;
RewriteCond %{HTTP_HOST} !newdomain\.co\.uk$ [NC]
RewriteRule ^(.*)$ http\:\/\/www\.newdomain\.co\.uk/$1 [R=301,L]
This succeeds in redirecting any incorrect domain to my new domain, but does NOT put me on the corresponding sub-page.
So, for example, I type in the following address ..;
http:\\old.com\calendar
... and I WANT to end up at;
http:\\newdomain.co.uk\calendar
But instead, I get directed to;
http:\\newdomain.co.uk\index.php
Can anyone help me get the redirect working to land me on the corresponding sub-page?
For the record, the Joomla site's SEO settings are as follows (and work as you would expect - site pages do not generally include index.php in the URL and correspond to the menu item alias);
Search Engine Friendly URLs - YES
Use URL rewriting - YES
Adds Suffix to URL - NO
Unicode Aliases - NO
The COMPLETE .htaccess file looks like this - I don't know if Joomla's .htaccess definitions are affecting the redirect that I've put in place;
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{QUERY_STRING} base64_encode[^(]*\([^)]*\) [OR]
RewriteCond %{QUERY_STRING} (<|%3C)([^s]*s)+cript.*(>|%3E) [NC,OR]
RewriteCond %{QUERY_STRING} GLOBALS(=|\[|\%[0-9A-Z]{0,2}) [OR]
RewriteCond %{QUERY_STRING} _REQUEST(=|\[|\%[0-9A-Z]{0,2})
RewriteRule .* index.php [F]
RewriteBase /
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{REQUEST_URI} !^/index\.php
RewriteCond %{REQUEST_URI} /component/|(/[^.]*|\.(php|html?|feed|pdf|vcf|raw))$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [L]
suPHP_ConfigPath /whatever/php.ini
RewriteCond %{HTTP_HOST} !newdomain\.co\.uk$ [NC]
RewriteRule ^(.*)$ http\:\/\/www\.newdomain\.co\.uk/$1 [R=301,L]
Header set X-UA-Compatible "IE=10"
Thanks in advance to anyone who can provide some insight!
You are rewriting a lot of stuff to index.php before already – so when your new Rules you added below that are evaluated, the (internal) request URI most likely is index.php already.
So move those new rules further up.

.htaccess rewrite to simultaneously change domain and remove path

My URL structure is currently as follows:
http://domain.com/folder/filename (CURRENT)
I want to change this so that I can use the following URL instead:
http://sub.domain.com/filename (NEW)
So accessing the CURRENT or the NEW url, should load the file located at the CURRENT url, but show the NEW url in the address bar. It should only apply to the "/folder/" path.
sub.domain.com is a mirror of domain.com, ie. they share the same file system and root directory.
This is what I have so far:
Options +FollowSymLinks
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www.)?domain.com$ [NC]
RewriteCond %{REQUEST_URI} ^/folder/?(.*)$ [NC]
RewriteRule ^(.*)$ http://sub.domain.com/$1 [R=301,L]
This is working, but is missing the rule to remove the "/folder/" from the path. I've tried combining multiple RewriteRule's with no luck. Any ideas? Thanks.
UPDATE: Thanks again #Gerben - I understand what your rules are doing now, but the second one isn't working for me. I suspect because it's conflicting with some other rewrite rules, in particular those of WordPress, which are lower down in my .htaccess file:
# BEGIN WordPress
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# END WordPress
Because of this the page ends up in a redirect loop, ie (from Chrome):
"The webpage at http://sub.domain.com/folder/index.php has resulted in too many redirects." - while the url I was originally trying to access was, for example, http://sub.domain.com/page
Any ideas?
Try:
RewriteCond %{HTTP_HOST} ^(www.)?domain.com$ [NC]
RewriteRule ^(folder/)?(.*)$ http://sub.domain.com/$2 [R=301,L]
This will redirect everything to sub.domain.com, and remove the /folder part of the URI if it is there. If not, it redirects and leaves the URI untouched.
RewriteCond %{THE_REQUEST} /folder/
RewriteRule ^folder/(.*)$ http://sub.domain.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^sub\.domain\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/folder/
RewriteRule ^(.*)$ folder/$1 [L]
# WordPress rules here
edit the second R=301 should not have been there
But this won't work, as wordpress has no way of knowing you want folder. You could add the Proxy flag to the rewrite, but then you need to change the rule above to not redirect on this internal proxy request.

redirect to subfolder

I have set up url redirect on my host--but its not working.
my htaccess looks like this now
rewriteengine on
rewritecond %{HTTP_HOST} ^.*$
rewriterule ^http:\/\/www\.bangkoksoftball\.info "http\:\/\/www\.bangkoksoftball\.info\/wordpress" [R=301,L] #4d3a8d4534e567
what i want is any request to http://www.bangkoksoftball.info to be redirected to http://www.bangkoksoftball.info/wordpress/
but any request to a path file or directory off the root not to be redirected
this so people can still access the old site via /home.html or index.html etc.. and still be able to navigate outside the wordpress folder
Try this in your .htaccess file:
RewriteEngine on
Options +FollowSymLinks
RewriteCond %{HTTP_HOST} ^(www\.)?bangkoksoftball\.info$ [NC]
RewriteRule ^/?$ /wordpress/ [R,L,NE]
Remember there is NO presence of http/https in %{HTTP_HOST} or %{REQUEST_URI} variables. Also . needs to be escaped in domain match. NE flag is for not escaping the query parameters.
The rewriterule line does not look at the domain, you must specify the domain the rule applies to in the RewriteCond line. In the rewriterule line, you specify first the paths that should trigger the rule, and the path the user should be sent to instead.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^http:\/\/www\.bangkoksoftball\.info$
RewriteRule ^/$ "http\:\/\/www\.bangkoksoftball\.info\/wordpress" [R=301,L]
This will send only requests to www.bangkoksoftball.info/ to the wordpress folder. If you want to also redirect www.bangkoksoftball.info/index.php to the wordpress folder, you would need to add an additional set of directives:
RewriteCond %{HTTP_HOST} ^http:\/\/www\.bangkoksoftball\.info$
RewriteRule ^/index\.php$ "http\:\/\/www\.bangkoksoftball\.info\/wordpress" [R=301,L]
This should work for you as it worked here:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?bangkoksoftball.info$ [NC]
RewriteRule ^/$ http://www.bangkoksoftball.info/wordpress/$1 [R=301,L]

Site Down For Maintenance

I have a site arranged as follows with subdomains as subdirectories:
/ [webroot]
/subdomain1/
/subdomain2/
I'd like to create an htaccess file that rewrites all accessed files to maintenance.php w/ 503 message, but I'm not sure why the following does not catch the subdirectories?
RewriteEngine On
RewriteBase /
RewriteCond %{REMOTE_ADDR} !^111\.222\.333\.444$
RewriteCond %{REQUEST_URI} !^/maintenance\.php$
RewriteRule ^(.*)$ /maintenance.php [L]
Do I have to call out each subdirectory something like...
RewriteRule ^/subdirectory1(.*)$ /maintenance.php [L]
RewriteRule ^/subdirectory2(.*)$ /maintenance.php [L]
This should work:
RewriteEngine On
RewriteCond %{REMOTE_HOST} !^111\.222\.333\.444
RewriteCond %{REQUEST_URI} !/maintenance.php$
RewriteRule $ /maintenance.php [L]
If you have additional htaccess files in your subdirectories, you have to watch for changes after you remove that file. If htacess controls rewrite, you may receive 404 errors when you view those pages.
I was expecting to see the maintenance page, not realizing I needed to have a different ip address according to the following code:
This code allows your ip to view the site and no one else:
RewriteEngine On
RewriteBase /
#ip is not your ip. Change ip to see maintenance page
RewriteCond %{REMOTE_ADDR} !^111\.222\.333\.444$
#requests made not to maintenance.php ...
RewriteCond %{REQUEST_URI} !^/maintenance\.php$
#rewrite to maintenance.php
RewriteRule ^(.*)$ /maintenance.php [L]