Add exception in .htacces rule - .htaccess

I'm using this rule in .htaccess to redirect requests to a subdirectory, say 'images', and anything inside it:
RedirectMatch 403 ^/images/.*$
However, the problem is that requests from my own domain are blocked too, so there are no images displayed on the site.
So I would like to insert an exception, for mydomain.com.
Any hints appreciated.

You can not add exception to your domain with RedirectMatch directive.
You need mod_rewrite here
RewriteEngine on
RewriteCond %{HTTP_HOST} !^(www\.)?domain.com$ [NC]
RewriteRule ^images/.*$ - [R=403,L]
The rule will return 403 forbidden error for /images except "domain.com".
Replace domain.com with yourdomain.com

Related

htaccess redirect repeats folder in URL over and over?

I'm trying to redirect camping.website.com to website.com/camping. I have an .htaccess file under the subdomain on my server with this rule:
Redirect 301 / https://website.com/camping/
With the slash on the end like above, the URL it returns is:
https://website.com/camping/camping/camping/camping/camping/camping/camping/
If I remove the slash on the end, it will return this instead:
https://website.com/campingcamping
If I change the destination URL to anything else, the redirect works normally. It's only when I have the word "camping" that it messes up. Is there some kind of conflict because the folder I'm trying to redirect to is the same as the subdomain?
Edit: I should mention something else. The following redirect:
Redirect 301 /category/camping-parks/ https://website.com/camping/camping-parks/
Returns this:
https://website.com/campingcamping/camping-parks/
Strange, huh?
Your problem is within this rule :
Redirect 301 / https://website.com/camping/
Will catch both domain and subdomain requests as you commented that they are in same server.
Try this using mod_rewrite:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?camping\.website\.com [NC]
RewriteRule ^(.*)$ https://website.com/camping/$1 [L,R=301]
Note: clear browser cache then test

redirect 301 not working properly in .htaccess

htaccess 301 redirect. I have this old site which is for example http://test.org/conference I want to redirect it to conference.test.org
What happened is the 301 redirect in my .htaccess file is quite buggy.
Here is my 301 redirect htaccess code below:
RewriteCond %{REQUEST_URI} ^/http://test.org/conference(/)?
RewriteRule ^(.*)$ http://conference.test.org/? [R=301,L]
When I test this one it runs and redirects correctly. But when I test it over and over again. It seems not to redirect anymore.
Can someone help me have a htaccess 301 redirect code?
Any help is much appreciated.TIA
I assume this is what you are looking for:
RewriteEngine on
RewriteRule ^conference/?$ http://conference.test.org/ [R=301,L,QSA]
Please note that %{REQUEST_URI} only contains the path of the URI, so not the protocol and the hostname. Reason is that the evaluation is performed inside a http host. This is explicitly pointed out in the documentation: http://httpd.apache.org/docs/current/mod/mod_rewrite.html
Depending on your http hosts setup you might also have to add a condition to prevent an endless redirection loop:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^conference\.test\.org$ [NC]
RewriteRule ^conference/?$ http://conference.test.org/ [R=301,L,QSA]
But usually that is not required, since the rule should be defined inside the test.org http host...

How to Permanent Redirect a Directory That No Longer Exists (via .htaccess)

I am having trouble redirecting an entire directory that no longer exists on our server.
All variations of the following are not working and I simply get a 404 Page Not Found.
.htaccess file looks like this:
redirect 301 /non_existent_directory/ http://my.website.com/existent_directory/
Is it possible to use the Redirect 301 directive for this? Or is this only solvable by mod_rewrite?
Thanks
I even tried:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?my\.website\.com\/non_existent_directory\/$ [NC]
RewriteRule ^(.*)$ http://my.website.com/existent_directory/ [R=301,L]
No luck...
From the Redirect documentation, I would say
Redirect 301 /non_existent_directory http://my.website.com/existent_directory
or
Redirect 301 /non_existent_directory /existent_directory
should work, provided you are allowed to use this in a .htaccess file. See also Troubleshooting .htaccess files. You should test without 301 though, to prevent caching of bad redirects by the client.
If this doesn't work, you can try a RewriteRule of course
RewriteEngine On
RewriteRule ^/?non_existent_directory(.*)$ /existent_directory$1 [R,L]
But this is equivalent to the above Redirect directive.

Prevent htaccess 301 redirects on sub-domain

I have 301 redirects which direct /index.html to /site folder and when i created a sub-domain it also redirects the link to /site which causes 404 Not Found
For example: members.mysite.com redirects to members.mysite/site which causes 404 error can I add execption for specific forlder or something without changing the redirect.
.htaccess content
AddType text/x-server-parsed-html .htm .html
RedirectMatch 301 ^/index.html(.*)$ /site$1
You can use mod_rewrite to check for the host:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^your-main.domain.com$ [NC]
RewriteRule ^index.html(.*)$ /site$1 [R=301,L]
Take a look at the RewriteCond docs for a better description of its functionality. You can tailor the regular expression (^your-main.domain.com$) to fit more along the lines of your needs, like adding a (www\.)? in front to handle an optional "www".

Redirect website with htaccess

RewriteEngine on
RewriteRule (.+) http://newdomain.com/$1 [R=301,L]
I am using Wordpress and this is my htaccess configuration file, everything seems to be working except this one. This URL:
http://olddomain.com/wp-content/uploads/2012/12/abc-1-thumb.jpg
Redirects to:
http://newdomain.com/403.shtml
Instead of:
http://newdomain.com/wp-content/uploads/2012/12/abc-1-thumb.jpg
Examples:
http://mister-gan.com/wp-content/uploads/2011/02/simple-and-clean-2.png
http://ganchinhock.com/wp-content/uploads/2011/02/simple-and-clean-2.png
May I know why?
RewriteEngine on
RewriteRule (.+) http://newdomain.com/$1 [R=301,L]
Redirect 301 http://olddomain.com/wp-content/uploads/2012/12/abc-1-thumb.jpg http://newdomain.com/403.shtml
Upon #Wige's comment and rephrasing your question it seems that there is a permissions problem in one of the subdirectories on the new domain, as 403 is a permissions error.

Resources