.htaccess redirect user upon specified HTTP HOST - .htaccess

Due to some internal problems and refusal from another part, I need a way to redirect ALL access from a specified domain. So far I've come up with the following:
RewriteCond %{HTTP_HOST} ^bad-domain.com [NC]
RewriteRule ^(.*)$ http://mydomain.com/bad-request.html
Which doesn't work. Worth noting is that I'm not good with rewrites, but I'm trying to learn.
I've sucessfully implemented this in PHP, but that requires the code in every project, which really isn't the way to go.
All suggestions, tips and answers are appreciated that puts me in the right direction.

I don't see a reason why this should not work. Please check these points again:
RewriteCond %{HTTP_HOST} ^bad-domain.com [NC,L]
RewriteRule ^(.*)$ http://mydomain.com/bad-request.html
As Alexander Støver pointed out:
RewriteEngine on
Then, if you put those rules in your servers configuration you have to restart the daemon. So probably something like
/etc/init.d/apache2 restart
Make sure you check the error logfile if the daemon complains about something. Should be something like this:
/var/log/apache2/error_log
or wherever you write your logs to.
If you put those rules into ".htaccess" files (why?) then make sure the server is actually configured to use those files and that you allow to override file paths:
AllowOverride: FileInfo
Use logging to debug the rewriting. There are two options for this provided by mod_rewrite:
RewriteLog
RewriteLogLevel

You also need to add a rule for images.
RewriteCond %{REMOTE_ADDR} !^187\.10\.226\.42
RewriteCond %{REQUEST_URI} !^/maintenance\.html$
RewriteCond %{REQUEST_FILENAME} !.(gif|jpe?g|png)$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/maintenance.html [R=307,L]

Related

Redirect all subfolder's to parent folder

Sorry, I do not understand in htaccess and all, what i do, dont work for me.
Please tell me how to redirect from all these subfolders (with one rule):
/about/vacancy/sys-admin/
/about/vacancy/lalala/
/about/vacancy/master/
/about/vacancy/lol/
To parent folder:
/about/vacancy/
What i try:
#1
RewriteRule ^about/vacancy/(.*)$ /about/vacancy/ [L,R=301]
#2
RewriteCond %{REQUEST_URI} !^/about/vacancy/.+$
RewriteRule .* /about/vacancy/ [L]
Your second variant should work, but the first approach is less complex. You just have to slightly adapt it:
RewriteEngine on
RewriteRule ^/?about/vacancy/.+$ /about/vacancy/ [L,R=301]
In case you are using a distributed configuration file (".htaccess") and not the real http server's host configuration you need to enable those first. Take a look at the documentation of the AllowOverride for that.

RewriteRule does not match if folder name set to "myfolder/"

Since we migrated to a new server, some of our pages are broken (404). Reason is we have 2 broken rewrite rules.
What's really strange is that they work if I change folder's name.
For example this work:
RewriteRule ^anything/([a-zA-Z0-9-]+)/$ page.php?var=$1 [L]
This doesn't:
RewriteRule ^myfolder/([a-zA-Z0-9-]+)/$ page.php?var=$1 [L]
I can't even find a trick to make 301 redirects, because my original "myfolder/" virtual folder never matches.
Any ideas what's going on? I was thinking it could be a rule override or something like that (as it's hosted on a multidom solution), but i don't have such rules in my main site at the root. It drives me crazy.
Thx!
In practice you probably want to do 2 things. Disable multiviews and also bypass rules if the request is a real directory.
Options -MultiViews #turn off automatic URI matching, can cause weirdness
RewriteEngine on
#stop here if the request is a real file or directory
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^myfolder/([a-zA-Z0-9-]+)/?$ /page.php?var=$1 [L]

Use .htaccess to load files from another folder

I have a good reason to do this. I feel this is the most cost effective way of providing an update.
This is my current url structure
/ <-- Contains Website
/cart
/cms
Boss wants the client to have an option to forego the website and instead load the cart in place of the website. The system is fairly old, the website and cart are completely different systems. We host a lot of websites from this single system. Configuring 2 systems for 2 different websites types will involve too much maintenence.
The least work solution would be to rewrite the /cart into /. I came up with the following.
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/cart/
RewriteCond %{REQUEST_URI} !^/cms/
RewriteRule ^(.*)$ cart/$1
This returns a server error. Adding /cart/$1 on the final line does work. I need this to work when the system isn't installed in the root directory. Does anyone know how to fix this?
You can use this rule:
RewriteEngine On
# Determine the RewriteBase automatically
RewriteCond %{REQUEST_URI}::$1 ^(.*?/)(.*)::\2$
RewriteRule ^(.*)$ - [E=BASE:%1]
RewriteCond %{REQUEST_URI} !/(cart|cms)/ [NC]
RewriteRule ^(.*)$ %{ENV:BASE}cart/$1 [L]

Multiple Conditions in .htaccess

I'm trying to redirect images on my server to a url, if the user client is NOT A BOT.
So far I have:
RewriteCond %{HTTP_USER_AGENT} "Windows" [NC]
RewriteCond %{REQUEST_URI} jpg
RewriteRule ^(.*)$ http://www.myurl.com/$1 [R=301,L]
But something is wrong. Is it possible to combine these 2 conditions?
Your idea is admirable, but the logic is flawed based on real world bot behavior.
I deal with security on sites all the time & User Agent strings are faked all the time. If have an option to install it, I would recommend using a tool like Mod Security. It’s basically an Apache module firewall that uses configurable rulesets to deny bad patterns of access behavior. But honestly, if you are having issues with .htaccess stuff like this Mod Security might be too intense to understand.
A better tact is to just prevent hot-linking via mod_rewrite tricks like this.
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?mydomain.com/.*$ [NC]
RewriteRule \.(gif|jpg)$ http://www.mydomain.com/angryman.gif [R,L]
Then again, reading your question I am not 100% sure what you want to achieve? Maybe mod_rewrite stuff like this can give you hints on how to approach the issue? Good luck!

how to achieve subdomain url rewrite using modrewrite

I want to redirect website user to www.mywebsite/users.php?user=xyz when the user types http://xyz.mywebsite.com into the address bar where http://xyz.mywebsite.com is virtual and doesnt exist.
I am a beginner and doesnt know much about url rewriting. Tried to search google and stack overflow but didnt got the solution.
I started with this
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^([^.]+).mobilehealthnig.com$
RewriteRule (.*) http://www.mywebsite.com/users.php?subdomain=%1
</IfModule>
also tried this but none of these are working for me
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.mywebsite.com
RewriteCond %{HTTP_HOST} ([^.]+).mywebsite.com
RewriteRule ^(.*)$ /users.php?subdoamin=%1
Ok, based on your additional input I suggest this setup:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.+)\.mywebsite\.com$
RewriteRule (.*) http://www.mywebsite.com/users.php?subdomain=%1 [L]
</IfModule>
If that does not work (I take that from your comment), you have to check where the actual problem is:
does rewriting work at all?
does this rule do anything atall? what?
is that rule ignored? why?
What absolutely makes most sense in such cases is to turn on rewrite logging to see and understand what is actually happening inside the rewrite engine. Please take a look at the two commands RewriteLog and RewriteLogLevel inside the manual mentioned. Enable a log level of maybe 7 and look what is logged when you make a single request. It should give you an idea of where to look for the cause of the problem.

Resources