Sinan
Sinan

Reputation: 11563

Having problems with a Rewrite rule

I have an application most of it is still in development that's why i need to block access on all pages but just not two of them.

Suppose i have a.php all requests will be redirected to here except b.php.

So i think there should be 3 rules:

1st: when a.php and b.php are requested they should be visible to user,

2nd: if anything other than those two is requested, 
     it should be redirected to a.php.

3rd: for external css,javascript and image files 
     there should be an access rule

Since i dont have much experience with server administration, i believe i'm totally lost:)

This is what i tried:

RewriteBase /
RewriteCond %{REQUEST_FILENAME} !^/b.php
RewriteRule !\.(js|ico|txt|gif|jpg|png|css)$ a.php

Upvotes: 0

Views: 409

Answers (2)

Gumbo
Gumbo

Reputation: 655489

In practice you would swap the second and third rule as your second rule would be the default route:

# 1st
RewriteRule ^(a\.php|b\.php)$ - [L]

# 3rd
RewriteRule \.(js|ico|txt|gif|jpg|png|css)$ - [L]

# 2nd
RewriteRule !^a\.php$ a.php [L]

The subsitution - means that the URI is not changed.

Upvotes: 2

NawaMan
NawaMan

Reputation: 932

I am not sure what you means about #3 but see from what you are trying, I guess you means, all js,ico,... are to be redirect to a.php. Is that right?

If so, that means, a => a.php (with parameter), b => b.php (with parameter) and other => a.php. So try this:

RewriteBase /
RewriteRule ^a\.php(.*) /a.php$1
RewriteRule ^b\.php(.*) /b.php$1
RewriteRule ^.*         /a.php

But if you means that all those script and media files are to be accessible normally, try this:

RewriteBase /
RewriteRule ^a\.php(.*)                          /a.php$1
RewriteRule ^b\.php(.*)                          /b.php$1
RewriteRule ^(.*)\.(js|ico|txt|gif|jpg|png|css)$ /$1.$2

Basically, the rewrite rule is the regular expression and $1,$2,... are match-string group (those wrapped with "(" and ")").

Hope this help.

Upvotes: 2

Related Questions