简体   繁体   中英

Redirect non-existant site pages via .htaccess

I have a problem right now with visitors browsing to pages that used to exist on my server but no longer. This is due to my initially poor decision to use a subdirectory instead of a subdomain. Because of this, people posted direct links to the pages themselve instead of the directory to various message boards. So when people Google and find links to my site, they click what the people have linked and it directs them to a "Not Found" page.

To better explain, here is my best attempt at giving an example.

The main URL people were using to visit my site was hxxp://example.com/subsite/. Within that /subsite/ directory there were files such as welcome.html, about.html, contact.html. So what happened is that when people started spreading my website links around, they would spread a link such as hxxp://example.com/subsite/welcome.html instead of just hxxp://example.com/subsite/. As of right now, the subsite directory only has (mainly) an index.html file.

What I am trying to accomplish is that whenever someone tries to navigate to any webpage within that subdirectory that doesn't exist, it redirects them to the index.html file. I'd like to accomplish this via the .htaccess file.

I attempted to do this myself by using the below code, but apparently I'm too new to how the .htaccess and apache modules work and the below code causes a constant redirect loop.

<IfModule mod_rewrite.c>
 RewriteEngine on
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteRule ^(.*)$ hxxp://example.com/subsite/ [L,QSA]
</IfModule>

If anyone has any input, or needs me to clarify any information just let me know. Any help would be greatly appreciated. Thank you!

You can use:

 RewriteEngine on
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteRule ^(subsite)/.+$ /$1/ [L,NC,R=301]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM