2

I need, in .htaccess, redirect all behind domain, but even with more slashes and with slash or no slash at the end, but with one exception.

For example

example.com/all/sites/should/redirect/to/one/file/
example.com/all/sites/should/redirect/to/one/file    

should go to

example.com/bye.php

but

example.com/exception

should work, no redirect.

I tried this.

# Turn rewriting on
Options +FollowSymLinks
RewriteEngine On
# Redirect all requests to bye.php
RewriteCond %{REQUEST_URI} !=/bye.php
RewriteCond %{REQUEST_URI} !.*\.png$ [NC]
RewriteCond %{REQUEST_URI} !.*\.jpg$ [NC]
RewriteCond %{REQUEST_URI} !.*\.css$ [NC]
RewriteCond %{REQUEST_URI} !.*\.gif$ [NC]
RewriteCond %{REQUEST_URI} !^/exception
RewriteRule .* /bye.php
RewriteEngine On
RewriteBase /

RewriteRule ^exception exception.php [QSA,L]

# Slash rules
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R]

It works, but seems only for 1 slash.

example.com/one-slash

When I enter this

example.com/more/slashes

it is not working, it is redirecting here.

example.com/bye.php/slashes/
2

I think you have two problems:

  1. You don't say your bye.php should redirect. Right now you have it specified as a rewrite rather than a redirect. You need to add [L,R] flags to it to get it to redirect.

  2. You don't need your slash rules at all anymore. Because you didn't specify "last" with the [L] flag, on your bye.php rule, the slash rules are also taking effect and causing unexpected behavior.

I think you would be fine with:

Options +FollowSymLinks
RewriteEngine On
RewriteBase /

# Redirect all requests to bye.php
RewriteCond %{REQUEST_URI} !=/bye.php
RewriteCond %{REQUEST_URI} !.*\.png$ [NC]
RewriteCond %{REQUEST_URI} !.*\.jpg$ [NC]
RewriteCond %{REQUEST_URI} !.*\.css$ [NC]
RewriteCond %{REQUEST_URI} !.*\.gif$ [NC]
RewriteCond %{REQUEST_URI} !^/exception
RewriteRule .* /bye.php [L,R]

RewriteRule ^exception exception.php [QSA,L]
  • Yes, it works. Thank you. – genderbee Nov 11 at 16:29

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.