skip to Main Content

I have an application on Codeigniter(3.1.11). Basically, I want a 404 redirect of URLs which have dashboard in URI. Like these:

dashboard/foo/bar
dashboard/foo-2/bar-2
dashboard/something
...

Also, I want to keep some exceptions in redirect rule so, some of the specific URLs which have dashboard as path URI should be excluded from this redirect. Let’s say I want to exclude some URLs like:

dashboard/new-foo/new-bar
dashboard/one-thing/abc
dashboard/another-thing/xyz

I have given a few tries but the exclude rule is not working. It redirects all URLs to 404. This is what I have in my .htaccess:

RewriteEngine on
RewriteCond %{REQUEST_URI} !^/(dashboard/new-foo/new-bar) [NC]  # Exclude this url (this condition is not working)
RewriteRule ^(.*)$ dashboard/$1 [R=404,L]

RewriteEngine on
RewriteCond $1 !^(index.php|resources|robots.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]

2

Answers


  1. I’m not expert with htaccess but I am pretty sure you need 2 RewriteCond lines, one to include all the dashboard urls and then the second to exclude the new ones. Each RewriteCond is implicitly joined with an "AND" so if you have many different patterns to exclude and you need a 3rd RewriteCode then you will need to join the 3rd with an "OR" on the 2nd condition.

    e.g.

    RewriteCond %{REQUEST_URI} ^/dashboard/(.*) [NC]
    RewriteCond %{REQUEST_URI} !^/dashboard/new-(.*) [NC]
    RewriteRule ^(.*)$ 404-page/$1 [R=404,L]
    

    There are a couple of other things I’d like to mention: 1) Your RewriteRule redirects back to a /dashboard URL so you could possibly end up in a continuous look here. 2) You don’t need to turn on Rewrite engine twice, once at the top is enough.

    If the rewrite rules in htaccess are getting complicated then perhaps you could use your index.php file to handle it (or some other method within Codeigniter).

    Login or Signup to reply.
  2. It can be done in a single rule with negative lookahead conditions like this:

    RewriteEngine on
    
    RewriteRule ^dashboard(?!/new-foo/new-bar|/one-thing/abc|/another-thing/xyz) - [R=404,NC,L]
    
    RewriteCond $1 !^(index.php|resources|robots.txt) [NC]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/$1 [L]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search