Home > other >  .htaccess redirect all but not two
.htaccess redirect all but not two

Time:01-27

I'm trying to redirect like this using .htaccess:

* -> index.html
/affiliate -> affiliate.html
/ref/* -> ref.html

But the problem is, that all links are redirected to index, how I can exclude several links from this rule?

I have this code for now:

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /
  RewriteRule ^index\.html$ - [L]
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule . /index.html [L]
</IfModule>

CodePudding user response:

With your shown samples/attempts please try following htaccess rules file.

Please make sure to clear your browser cache before testing your URLs.

RewriteEngine ON
##Rules for uris which starts from affiliate rewrite to affiliate.html here..
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^affiliate/?$ affiliate.html [QSA,NC,L]

##Rules for uris which starts from ref rewrite it to ref.html here..
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ref  ref.html [QSA,NC,L]

##Rules for rest of the non-existing pages to rewrite to index.html here...
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.html [L]

CodePudding user response:

Have your rules like this instead:

Options -MultiViews
DirectoryIndex index.html

RewriteEngine On

# Prevent further processing if request maps to a static resource
RewriteRule ^((index|affiliate|ref)\.html)?$ - [L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule . - [L]

# /affiliate -> affiliate.html
RewriteRule ^affiliate$ affiliate.html [L]

# /ref/* -> ref.html
RewriteRule ^ref/ ref.html [L]

# * -> index.html
RewriteRule . index.html [L]

You do not need the RewriteBase directive here. (Or the <IfModule> wrapper.)

  •  Tags:  
  • Related