htaccess redirect everything except index page - .htaccess

I have removed the blog from my domain and put a simple index.html instead.
How do I redirect all the incoming traffic so that it does not show a 404 error, but redirects to the index?
I tried this, but it loops...
RewriteEngine on
RewriteRule ^(.*)$ /index.html [L,R=301]

Try:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/index.html
RewriteRule ^(.*)$ /index.html [L,R=301]
As you mentioned that this doesn't work, I'd try:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/aaa.html
RewriteRule ^(.*)$ /aaa.html [L,R=301]
index.html is a general default file name, so there might be rules that are on the server level, not in your .htaccess. That depends on the server setup, though.

Try adding the
Options +FollowSymLinks -MultiViews -Indexes
at the very top and give it a try.

Related

Redirect entire subdirectory to a domain

I am trying to redirect an entire subdirectory to our main domain.
Basically links like this:
https://garrysun.com/dev/ayurveda-products/categories/ayurvedic-ghee-clarified-butter?limit=15
https://garrysun.com/dev/
https://garrysun.com/dev/ayurveda-products/
Should all go to
https://garrysun.com/
We are using OpenCart 2.3.0.2
I have tried:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/dev/(.*)$
RewriteRule ^(.*) https://garrysun.com/ [R=301,NC]
and
RewriteRule ^dev/(.*)$ https://garrysun.com [R=301,NC,L]
But neither seems to work. What is the best .htaccess rule to make this work?
Inside /dev/.htaccess have this single rule:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?:www\.)?garrysun\.com$ [NC]
RewriteRule ^ https://garrysun.com/? [R=301,L]
Your web server appears to be Nginx as per the response headers. For Nginx use this rule:
location ~ ^/dev/ {
return 301 https://garrysun.com/?;
}
I used this redirect:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^(.*)$ /$1 [R=301,L]
The problem is that if the page doesn't exist in the main website I get a 404 error.
For example:
This: https://garrysun.com/dev/777-oil-psoriasis
Becomes this: https://garrysun.com/777-oil-psoriasis
I get a 404 error because that page doesn't exist on the main site.
This works:
https://garrysun.com/dev/ayurveda-products/
Becomes:
https://garrysun.com/ayurveda-products/
because the new site has that page.

Redirect root (and only root) URL to another domain?

I'm looking to redirect my domain example.com and www.example.com to http://example2.com, but I want to keep all visits to my posts at http://example.com/sdfhs to continue through to their destinations.
Essentially, I ONLY want to redirect the root and www domain and leave the rest untouched.
How can I do this with .htaccess?
Don't know if you still have this issue, but I have had it on one of my domains (Yourls installation). Try:
RewriteEngine On
RewriteRule ^$ http://www.example.com/ [R=301,L]
It works on my installation, but check it out on your site.
Why not
RewriteEngine On
RewriteCond ${HTTP_HOST} example\.com|www\.example\.com
RewriteRule ^/?$ http://example2.com [L]
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond ${HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteRule ^$ http://example2.com [L,R=301]
You could simply match any URL with at least 1 character (.+) and mark that as the last rule for the rewrite engine to look at [L].
Then afterwards redirect anything else (.*) to the new domain (the only other thing that wouldn't be matched up to that point would be the root).
RewriteEngine On
RewriteRule ^(.+)$ - [L]
RewriteRule ^(.*)$ http://example2.com/$1 [R=301,L]

htaccess rewrite if no rewrite was provided

First off I'm not an expert in htaccess stuff, but i try to accomplish the following for some SEO fixes.
When the url is loaded, without any params/rewrite it should get some data attached before it continues.
For example:
http://www.domain.com >>> http://www.domain.com/en/
I thought the rewrite was right like this, but didn't work (500)
RewriteCond %{REQUEST_URI} !^(en|nl|de|etc)$
RewriteRule ^(.*)$ /en/ [L,R=301]
added
RewriteRule ^(/?[^/]+) /index.php?rewrite=1 [L] # tell php we got a rewrite
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.*)$ $1/en/ [NC]

.htaccess: Redirect root url to subdirectory, but keep root URL structure

I have created a site in a subdirectory and would like the site to appear as if it's in the root.
I used the below Mod_Rewrite code to get the site root to redirect to the subdirectory, but I would like the folder the files are held in to not appear.
currently: www.example.com/sitefiles/content/
Would like: www.example.com/content
Thanks
Options +FollowSymlinks -MultiViews
RewriteEngine on
RewriteBase /
RewriteRule ^$ /sitefiles/ [L]
RewriteRule (.*) /sitefiles/$1 [L]
If it needs to be done via .htaccess and mod_rewrite, then use this:
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/sitefiles/
RewriteRule (.*) /sitefiles/$1 [L]
Since you do not explicitly specify the page when website root will be hit, then there is no need for this line: RewriteRule ^$ /sitefiles/ [L]
You need to add condition for your main rewrite rule to prevent rewrite loop (when already rewritten URL gets rewritten again): RewriteCond %{REQUEST_URI} !^/sitefiles/
Well, the directory should have no reason to appear in any listing, and that mod_rewrite you posted should allow you to do as you said. Still, I would go about it a different way:
Options +FollowSymLinks +Indexes
RewriteEngine on
RewriteBase /
RewriteRule ^$ sitefiles/ [L]
RewriteRule ^(.+)$ sitefiles/$1 [L]
Hope this works.

Redirect all pages except one page to sub-domain htaccess

I have a index.php in my main domain root
domain.com/index.php
And ive moved my forums which was in the same "root" to a subdomain
forums.domain.com
I need to Redirect everything except the index.php
ive tryed loads of ways and none seem to work
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{SCRIPT_FILENAME} !index\.php$
RewriteRule ^(.*)$ http://forums.domain.com [L,R]
RewriteEngine On
RewriteCond %{HTTP_HOST} animelon\.com [NC]
RewriteCond %{REQUEST_URI} !^index\.php$
RewriteRule ^(.*)$ http://forums.domain.com/$1 [R=301,L]
If anyone has any ideas that would be great
as for the above codes I would them googling about.
Cheers
You may use RedirectMatch instead of rewriting, that is, replace all the rewrite block you are showing with:
RedirectMatch ^(/(?!index\.php).*) http://forums.domain.com$1
You can see the full explanation of the regex on Regexr here. In brief, it sends all the URIs NOT beginning with /index.php to forums.domain.com.
If you don't need any other rewrite rule, you can turn off rewriting by removing all the lines beginning with "Rewrite" from your .htaccess.

Resources