There are plenty of HTACCESS maintenance mode redirects here on stack overflow but I have yet to find one which will work for my structure.
I would like to redirect www.example.com to www.example.com/sub/maintenancemode.html. However I would still like to have access to subdomain.example.com. Subdomain will be used for dev and staging purposes etc
Here is your .htaccess file:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^subdomain\.example\.com$
RewriteRule ^.* /sub/maintenancemode.html?
Ended up using this in the end
RewriteEngine On
RewriteBase /
RewriteRule ^(dev|staging)($|/) - [L]
RewriteCond %{REQUEST_URI} !/maintenance/coming-soon.html$ [NC]
RewriteCond %{REQUEST_URI} !\.(jpe?g?|png|gif) [NC]
RewriteRule .* /maintenance/coming-soon.html [R=302,L]
Related
I'm having some issues with redirecting a clients website.
I have a site with a /uk directory that's being redirected to a uk sub-domain, Everything else redirects to a new URL
This all works fine, but they now need to access the old sites /uk/administrator directory. How can I exclude /uk/administrator from the other rules?
Here is my current set-up:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?example.com\.com$
RewriteRule ^uk/(.*) http://uk.example.com/$1 [R=301,L]
RewriteEngine on
RewriteBase /
RewriteRule ^(.*)$ http://www.newexample.com/ [R=301,L]
I've been frantically Googeling for the past 30min with no luck.
Try :
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?example.com\.com$
#Exclude /uk/administ
RewriteCond %{REQUEST_URI} !^/uk/administrator/
RewriteRule ^uk/(.*) http://uk.example.com/$1 [R=301,L]
You can use negative lookahead:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?example.com\.com$ [NC]
RewriteRule ^uk/((?!administrator/).*)$ http://uk.example.com/$1 [R=301,L]
RewriteRule ^(.*)$ http://www.newexample.com/$1 [R=301,L]
I need to prevent direct access to a URL (http://www.example.com/gated-asset). Is there any way to add code to the htaccess file that would redirect all direct traffic to another page (http://www.example.com/form)?
I have tried the following code in my htaccess file, but all pages, including the home page, redirect to www.example.com/form.
RewriteEngine On
RewriteCond %{HTTP_REFERER} !^http://go.example.com [NC]
RewriteRule .* http://www.example.com/form [R,L]
The entire .htaccess file looks like this (it is a WordPress site):
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
RewriteEngine On
RewriteCond %{HTTP_REFERER} !^http://go.example.com [NC]
RewriteRule .* http://www.example.com/form [R,L]
I have also tried the following:
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^http://go.example.com [NC]
RewriteRule ^/$ /form/ [R,L]
RewriteRule ^/$ /gated-asset/ [R,L]
As well as:
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^http://go.example.com [NC]
RewriteRule http://www.example.com/gated-asset http://www.example.com/form/ [R,L]
You need to include http:// in the referrer check if you are matching the beginning (^). Unlike HTTP_HOST, HTTP_REFERER requires it. In addition, the referring URL may include a REQUEST_URI, and so closing the expression at the domain will prevent it from working.
RewriteEngine On
RewriteCond %{HTTP_REFERER} !^http://go.example.com [NC]
RewriteRule .* http://www.example.com/form [R,L]
If you don't want to include the http://, then you can use the following condition instead:
RewriteCond %{HTTP_REFERER} !go.example.com [NC]
However, I would recommend the first example be used.
Also included here are the R and L flags. R is implied if the rewrite is to an external resource, but it is generally better to include it.
I have a subfolder in which I am running a wsgi app. I also have an addon domain pointing to this subfolder. I have trouble setting up the .htaccess file in the subfolder.
My current file structure:
public_html/
.htaccess
irrelevant index.html
some other irrelevant stuff
sub_folder/
.htaccess
app.wsgi
I have a maindomain.com pointing to /public_html/, and an addondomain.com pointing to /sub_folder/.
Currently the .htaccess file in the root folder is empty, the one in sub_folder is:
RewriteEngine On
RewriteBase /
RewriteRule ^(app\.wsgi/.*)$ - [L]
RewriteRule ^(.*)$ app.wsgi/$1 [QSA,L]
This works if I go to addondomain.com, but gives me a 404 when I go to maindomain.com/sub_folder. The log shows that it looks for /public_html/app.wsgi then, instead of /public_html/sub_folder/app.wsgi. How can I rewrite the .htaccess to make it work in both scenarios?
This should work with add-on domain as well.
RewriteEngine On
RewriteBase /
RewriteRule ^(app\.wsgi/.*)$ - [L]
RewriteCond %{HTTP_HOST} ^maindomain.com$ [NC]
RewriteRule ^sub_folder/(.*)$ sub_folder/app.wsgi/$1 [NC,QSA,L]
RewriteCond %{HTTP_HOST} ^addondomain.com$ [NC]
RewriteRule ^(sub_folder/)?(.*)$ app.wsgi/$1 [NC,QSA,L]
EDIT :
The rules above need to be tweaked since OP's .htaccess is already within /sub_folder. The ones above would still work if anyone wants their root /.htaccess to be in the driving seat.
RewriteEngine On
RewriteBase /
RewriteRule ^(app\.wsgi/.*)$ - [L]
RewriteCond %{HTTP_HOST} ^maindomain.com$ [NC]
RewriteRule ^(.*)$ sub_folder/app.wsgi/$1 [NC,QSA,L]
RewriteCond %{HTTP_HOST} ^addondomain.com$ [NC]
RewriteRule ^(.*)$ app.wsgi/$1 [NC,QSA,L]
I made it work, sort of...
RewriteEngine On
RewriteBase /
RewriteRule ^(app\.wsgi/.*)$ - [L]
RewriteCond %{REQUEST_URI} ^/sub_folder [NC]
RewriteRule ^(.*)$ sub_folder/app.wsgi/$1 [QSA,L]
RewriteCond %{REQUEST_URI} !^/sub_folder[NC]
RewriteRule ^(.*)$ app.wsgi/$1 [QSA,L]
#Ravi's answer works with addondomain.com, but not with maindomain.com/sub_folder, because in that case the index is showed, because it is not redirected to app.wsgi.
To solve this, I added a second RewriteCond and RewriteRule. The problem now is, it doesn't work when you go to addondomain.com/sub_folder (although that shouldn't happen). So if there is a better answer that also handles that properly, please enlighten me. Otherwise I'll just hope that that situation doesn't happen.
Edit:
This works:
RewriteEngine On
RewriteBase /
RewriteRule ^(app\.wsgi/.*)$ - [L]
RewriteCond %{HTTP_HOST} ^maindomain.com$ [NC]
RewriteRule ^(.*)$ sub_folder/app.wsgi/$1 [NC,QSA,L]
RewriteCond %{HTTP_HOST} ^addondomain.com$ [NC]
RewriteRule ^(.*)$ app.wsgi/$1 [NC,QSA,L]
Ravi's exact code didn't work, because this .htaccess is in the sub_folder, so the ^(.*)$ part is only the stuff after sub_folder/, somehow. But modified like this it works. Thanks for the help.
I have a site that needs to exist in a subfolder
example.com/site
But i'm trying to use the .htaccess to remove any links that contain www (to make sure codeigniter csrf doesn't throw errors), so i've added
RewriteCond %{HTTP_HOST} ^(www\.example\.com)?$
RewriteRule ^(.*)$ http://example.com/site/$1 [R=301,L]
This works well when there is a page identifier specified, so
www.example.com/site/book rewrites to example.com/site/book
But when there is no page identifier specified I get a 404
www.example.com/site rewrites to example.com/site//usr/local/pem/vhosts/103480/webspace/httpdocs/new
I was wondering if anybody could point me in the right direction?
This is my full .htaccess
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.example\.com)?$
RewriteRule ^(.*)$ http://example.com/site/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
You may try this instead:
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{REQUEST_URI} ^/(.*) [NC]
RewriteRule .* http://example.com/%1 [R=301,L]
Maybe, you're just missing a RewriteBase
Depending on where the .htaccess file is, try either
RewriteBase /
or
RewriteBase /site
Never test with 301 enabled, see this answer
Tips for debugging .htaccess rewrite rules
for details.
Domain based redirect to different PHP files via htaccess
Hello there.
Here's the thing:
I have two domains here [http://www.myproject.com and
[http://www.myproject.com.br .. In my root folder i have all the stuff for
my projects including two "indexes": "index-en.php" for english and
"index.php" for portuguese.
Can i have some trick in htaccess that redirect my users to one
of these files depending on domain? .. something like:
[http://www.myproject.com [OR]
[http://myproject.com redirect to [http://www.myproject.com/index-en.php
and
[http://www.myproject.com.br [OR] [http://myproject.com.br redirect to
[http://www.myproject.com.br/index.php
??
Sorry if this is a stupid question but im almost crazy looking for tutorials over
the internet and i cant get something that works or some response if this is possible or just a stupid question .. Can someone please give me some direction?
Maybe some solution with a conditional directoryIndex (I dont know if that's possible).
thanks a lot
Actually i have this in my .htaccess:
ErrorDocument 404 /404.php
<IfModule mod_rewrite.c>
#Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^myproject.com$ [NC]
RewriteRule ^(.*)$ http://www.myproject.com/index-en.php [R=301,L]
RewriteCond %{HTTP_HOST} ^myproject.com.br$ [NC]
RewriteRule ^(.*)$ http://www.myproject.com.br/ [R=301,L]
</IfModule>
This just does the "non www to www" - that works correctly
This should do what you want. The RewriteCond trickery is probably a bit overkill here, but it does make adding extra languages easier:
ErrorDocument 404 /404.php
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{HTTP_HOST}/index.php \.com\.br/(index.php)$ [NC,OR]
RewriteCond %{HTTP_HOST}/index-en.php \.com/(index-en.php)$ [NC]
RewriteRule ^ %1
RewriteCond %{HTTP_HOST} myproject\.com$
RewriteRule ^/$ /index-en.php
RewriteCond %{HTTP_HOST} myproject\.com\.br$
RewriteRule ^/$ /index.php