Prevent htaccess 301 redirects on sub-domain - .htaccess

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".

Related

htaccess redirect all pages ending with .html to without html Except some pages

I'm having an issue with my .htaccess rules for redirection.
I need to redirect pages ending with .html to the pages ending with slash.
/about-us.html to /about-us/
But there are also pages where I need to redirect to specific page without .html like
/gallery/first-gallery/this-is-the-gallery.html to /gallery/new/
The problem is that my rules are not working together nicely, it seems that the rule to redirect .html pages ignores all other rules.
This is how my .htaccess looks like
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^/?(.*).(html)$ /$1 [R=301,L]
Redirect 301 /about-us/photo-gallery/17577-album-mca-corona-clay-tile-roof-princeton-texas/131173-58e29f485064eprinceton-texas-roofing-services-1.html /about-us/photo-gallery/17577-album-mca-corona-clay-tile-roof-princeton-texas/
Redirect 301 /about-us/photo-gallery/17577-album-mca-corona-clay-tile-roof-princeton-texas/131174-58e2a063b8b06princeton-texas-roofing-services-2.html /about-us/photo-gallery/17577-album-mca-corona-clay-tile-roof-princeton-texas/
#Many other 301 rules below
</IfModule>
So when I type in domain.com/about-us/photo-gallery/17577-album-mca-corona-clay-tile-roof-princeton-texas/131173-58e29f485064eprinceton-texas-roofing-services-1.html in the browser it redirects to domain.com/about-us/photo-gallery/17577-album-mca-corona-clay-tile-roof-princeton-texas/131173-58e29f485064eprinceton-texas-roofing-services-1 where I need this specific redirect to redirect to /about-us/photo-gallery/17577-album-mca-corona-clay-tile-roof-princeton-texas/
I tried moving the RewriteRule ^/?(.*).(html)$ /$1 [R=301,L] to the bottom hoping that the rules above will process first, but that did not work.
What can I do to make this work as I need it?
First of all if there are mod_alias like redirect and mod_rewrite like RewriteRule mod_rewrite rules get executed before mod_alias rules.
There are many senarios to sove this issue like excluding all other redirect rules from the main rewriterule but it think if the other rules redirect have same target to only ommit last part of URI you could make rules like the folwoing :
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^([^\/]+)/([^\/]+)/([^\/]+)/([^\/]+)\.(html)$ /$1/$2/$3 [R=301,L]
RewriteRule ^(.*)\.(html)$ /$1 [R=301,L]
</IfModule>
Rules above will check if URI is coming in this form /something/something/something/something.html like /about-us/photo-gallery/17577-album-mca-corona-clay-tile-roof-princeton-texas/131173-58e29f485064eprinceton-texas-roofing-services-1.html then redirect it into /something/something/something and if not coming in this way , the second rule will be applied.
Note: clear browser cache then test

Redirect specific URL to another URL on another subdomain

I have 5 URLs on a subdomain website http://subdomain.example.com, and I want them to redirect to 5 other URLs on my main website, https://www.example.com/.
Important: URLs do NOT have the same structure!
Example:
http://subdomain.example.com/url1 should redirect to https://www.example.com/ipsum
http://subdomain.example.com/url2 should redirect to https://www.example.com/lorem
etc.
How can I handle that?
UPDATE:
There is a play folder (name of the subdomain) which contains the subdomain website files and a htdocs folder which contains the www website files.
Here is the .htaccess file in my play folder:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Since the subdomain has it's own .htaccess file, you don't need to specify the hostname as part of the redirect. And since you already have mod_rewrite directives in the subdomain's .htaccess file, you should also use mod_rewrite for these redirects (to avoid conflicts). Otherwise, you'll need to specify these redirects one-by-one.
Try the following at the top of your subdomain's /play/.htaccess file. Note that this needs to go before the existing directives in the file.
# Specific redirects
RewriteRule ^url1$ https://www.example.com/ipsum [R=302,L]
RewriteRule ^url2$ https://www.example.com/lorem [R=302,L]
The above would match a request for http://subdomain.example.com/url1 and redirect accordingly, etc.
Note that the RewriteRule pattern (regular expression) does not start with a slash when used in a per-directory (.htaccess) context.
Note that these are 302 (temporary) redirects. Change them to 301 (permanent) - if that is the intention - only once you have confirmed they are working OK (to avoid caching issues).
Try this...
Redirect 301 http://subdomain.example.com/url1 https://www.example.com/ipsum
Redirect 301 http://subdomain.example.com/url2 https://www.example.com/lorem

301 Redirect a folder to a single page

I have a folder on my website that has been superceded and I want to redirect an attempt to access any file in this folder to the current home page.
I have read many questions here but none seem to do exactly this.
I have tried various things in .htaccess but it seems to always append the filename to the redirect address.
redirect 301 old/$ www.example.com/index.html
redirect 301 old/ www.example.com/index.html
redirect 301 ^old/ www.example.com/index.html
I have seen various mentions of RewriteRule. Do I need to use that?
You can use this code in your /old/.htaccess file:
RewriteEngine On
RewriteRule ^ http://www.example.com/index.html [L]
OR from root .htaccess use:
RewriteEngine On
RewriteRule ^old(/.*)?$ http://www.example.com/index.html [L,NC]

I need to remove /en from all URLs, would like to use .htaccess

Using Joomla and trying to stop using JoomFish, but for the time we did use it, other sites linked to our pages as "basedomain.org/en/everypage". Now those links go to 404 errors.
How do I use .htaccess to rewrite or redirect all requests for basedomain.org/en/everypage to basedomain.org/everypage??
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteRule ^en/(.*)$ /$1 [L,R=302]
Once you've confirmed it works, change 302 to 301. (301 is very hard to debug because browsers cache it)
Or, use a RedirectMatch (simpler, and no need to enable the RewriteEngine):
RedirectMatch permanent /en/(.*) http://www.example.com/$1
(replace www.example.com with your own domain)

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.

Resources