Help with htaccess 301 redirects

The wordpress site I'm working got moved into a subdirectory therefore all the links from other sites don't work anymore. I implemented a 301 redirect with htaccess which is great because it fixes that problem BUT the old root directory now has an index.html that has landing page my client absolutely wants to be seen.

So, how can I set up my htaccess to redirect all traffic into the sub directory (to fix the incoming links) EXCEPT the index.html in the root directory because it has the landing page.

I don't know how htaccess works well but this is what I have right now.

Order deny,allow
ErrorDocument 404 /404.php
DirectoryIndex index.php index.html

RewriteEngine On
RewriteCond %{REQUEST_URI} !^/$ [OR]
RewriteRule ^.*$ http://example.com/portal/$0 [R=301,L]

So to recap:

Right now EVERYTHING redirects from the root to the subdirectory. I want everything to redirect EXCEPT for index.html in the root directory because it's a landing page.

Thanks!!

5 Replies

Would this do it?

RewriteEngine On
RewriteCond %{REQUEST_URI} !^/$ [OR]
# this should only grab the root dir's index.html and ignore index.html in subdirectories
RewriteCond %{SCRIPT_URI} !^example.com/index.html$ 
RewriteRule ^(.*)$ http://example.com/portal/$1 [R=301,L]

MSJ

thanks for that but unfortunatly it just forwards everything right to portal still.

this so far is best I have:

RewriteEngine On
RewriteCond %{REQUEST_URI} !^/index\.(php|html) [NC]
RewriteRule ^.*$ http://example.com/portal/$0 [R=301,L]

but it only works if i specifically write example.com/index.html

It's the only file in there and the irectoryIndex index.html is set but if i just go to the root with nothing else in the url, it forwards me to /portal/

So, I'm close? hahaha. any thoughts?

So if I understand right, you want to redirect everything (except index.php&index.html) from http://example.com/ to http://example.com/portal.

try following:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteCond %{REQUEST_URI} !^/index\.(php|html)$ [NC]
RewriteRule ^(.*)$ http://example.com/portal/$1 [R=301,L]

besides, you said you are using wordpress, so I assume you removed previous rewrite entries for wordpress in your root directory.

@Tamerax:

thanks for that but unfortunatly it just forwards everything right to portal still.

this so far is best I have:

RewriteEngine On
RewriteCond %{REQUEST_URI} !^/index\.(php|html) [NC]
RewriteRule ^.*$ http://example.com/portal/$0 [R=301,L]

but it only works if i specifically write example.com/index.html

It's the only file in there and the irectoryIndex index.html is set but if i just go to the root with nothing else in the url, it forwards me to /portal/

So, I'm close? hahaha. any thoughts?

That is exactly what I want. Unfortunately, that bit of code doesn't work unless I specify the index.html like before. Still forwards on anything else.

And yes, the directory is empty except for the htaccess file, the index.html and the subdirectory folder.

RewriteEngine On RewriteCond %{REQUEST_URI} !^/index\.(php|html) [NC] RewriteCond %{REQUEST_URI} !^/$ {NC] RewriteRule ^.*$ http://example.com/portal/$0 [R=301,L]

Still not working. everything is forwarding to /portal/ unless it has it specifically written index.html

Reply

Please enter an answer
Tips:

You can mention users to notify them: @username

You can use Markdown to format your question. For more examples see the Markdown Cheatsheet.

> I’m a blockquote.

I’m a blockquote.

[I'm a link] (https://www.google.com)

I'm a link

**I am bold** I am bold

*I am italicized* I am italicized

Community Code of Conduct