1

How can I edit my .htaccess file to redir everything in a folder, except 3 pages on that same folder

Redir everything here: /site/ but exclude /site/page-1.html /site/page-2.html /site/page-3.html

my htaccess

# Redirect to site page
<IfModule mod_rewrite.c>
RewriteCond %{SCRIPT_FILENAME} !-d  
RewriteCond %{SCRIPT_FILENAME} !-f   
RewriteRule ^site/(.*)$ ./site/index.php
</IfModule>

Those files dont exist as physical files...

Here is the situation, I mess up with my canonical tags, then google indexed a lot of wrong pages in that time... (around 1300 pages) of those 1300 pages only 3 pages is the correct ones.... Thats why I wanna keep these 3 pages and redirect to a 404 page everything else

  • 1
    Do the 3 files /site/page-1.html etc. exist as physical files? To clarify, you are wanting to modify your existing rule block or create an additional redirect? Your exitsing rule block is an internal rewrite, not a redirect - hence the confusion. And what are you wanting to redirect to exactly? To /site/? – MrWhite Mar 27 at 22:43
  • 1
    Why don't you just change your index file to return a 404 status except for those 3 URLs? – Stephen Ostermiller Mar 28 at 12:13
1

...and redirect to a 404 page everything else

You shouldn't "redirect" to a 404 - simply serve the 404 directly.

For example, the following should go before your existing directives:

ErrorDocument 404 /custom-errors/e404.php

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_URI} !^/site/page-1\.html$
RewriteCond %{REQUEST_URI} !^/site/page-2\.html$
RewriteCond %{REQUEST_URI} !^/site/page-3\.html$
RewriteRule ^site/ - [R=404]

For any direct request that starts /site/ (except for the 3 URLs mentioned) then serve a 404. Edit /custom-errors/e404.php (or your existing Apache 404 document if you have one) to customise the response.

The first condition that checks against the REDIRECT_STATUS environment variable ensures that we only process direct requests and not rewritten requests by the front-controller below.

However, this means you are only accepting 3 URLs to your front-controller (the code you posted in the question). ie. You only rewrite these 3 URLs to /site/index.php. So, instead of the above, you could simply make sure that the request is one of these 3 URLs before rewriting.

For example, modifying your existing front-controller:

# Redirect to site page
<IfModule mod_rewrite.c>
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{REQUEST_URI} ^/site/page-1\.html$ [OR]
RewriteCond %{REQUEST_URI} ^/site/page-2\.html$ [OR]
RewriteCond %{REQUEST_URI} ^/site/page-3\.html$
RewriteRule ^site/(.*)$ ./site/index.php
</IfModule>

This is the opposite logic of the first solution. It rewrites only when it is one of the specified URLs, instead of redirecting when it isn't.

Or, as @Stephen suggested in comments, "change your index file [ie. index.php / front-controller] to return a 404 status except for those 3 URLs", and not change your .htaccess file at all. I'm curious why your site isn't already returning a 404 for these (presumably invalid) URLs?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.