Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.


I already asked a question about redirecting users with a certain IP http://serverfault.com/questions/192515/apache-allow-or-redirect-users thanks to dmah it works perfectly.
However I wanted to go further and not only restrict/allow a special folder but being able to add another rule (like presented here: http://www.kavoir.com/2010/02/use-php-to-handle-all-incoming-url-requests-in-a-seo-friendly-manner.html)
Here's my .htaccess:

RewriteEngine on
# Define the Error Document Path
ErrorDocument 404 /404.php
# Condition for the Rewriting rule: IP NOT starting with 1.2.3.4 (example)
RewriteCond %{REMOTE_ADDR} !^1\.2\.3\.4
# Condition is matched -> redirect 404 error doc
RewriteRule ^administration/(.+) [R=404,L]

#SEO modification
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d 
RewriteRule /$ /index.php [L]

If I don't add the SEO modification part it works. As soon as I try to enter in the /administration directory I got a 404 redirection. But if I add the SEO the first rule doesn't work anymore I mean the [L] flag is not used.
I tried with S=1 instead of L but the SEO does work then even for /administration :(
I can't find a good page with some explanations for the S=X condition making a kind of if-then-else statement nor I don't understand why with the L flag on the first rule it continues to parse the configuration file.
To be more clear:
I have a file / folder structure like:

/
/administration
/administration/secret/
/administration/index.php
/article
/article/test
/article/test/cool.html
/index.php
/.htaccess

And I want the /index.php to handle ALL the urls except the one that are for the administration folder (handled in the /administration/index.php file only if I'm in the correct IP/range). Which means: http : //www.foo.com/article/test/cool.html is sent to apache which Rewrite the URL to /index.php and with some explode() php function I got the parameters article and test and cool.html.

Problem... when I type http : //www.foo.com/administration/ ... it's handled by /index.php even outside of the allowed IP! Even with the L flag of the RewriteRule regarding the administration folder... I tested a lot of combinations:

  • adding S=1 instead of L for the rule with the IP ... no luck
  • adding RewriteRule ^administration/$ /administration/index.php [L] before the last rewriting rule works but only for /administration, if I put /administration/secret ... it's handled by /index.php :@
  • and tons of other stuff that just gave a cool Internal Server Error

Many thanks for help and ideas :-)

Great thanks again to dmah!! Here's my working like a charm .htaccess:

RewriteEngine on
# Define the Error Document Path
ErrorDocument 404 /404.php
# Condition for the Rewriting rule: IP NOT starting with 1.2.3.4 (example)
RewriteCond %{REMOTE_ADDR} !^1\.2\.3\.4
# Trying to access administration pages.
RewriteCond %{REQUEST_URI} ^/administration
# Redirect to the 404 page.
RewriteRule .+ /404.php [R=404,L]

#SEO modification
#RewriteBase /
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_URI} !^/administration
RewriteRule /$ /index.php [L]
share|improve this question
    
If you enable logging of rewrite activity you'll probably get a much better sense of what's going on. This means adding RewriteLog and RewriteLogLevel entries. Higher values for RewriteLogLevel generate more output; I usually start with RewriteLogLevel 5. –  larsks Nov 1 '10 at 13:51
    
Thanks but I've tried adding RewriteLog "/home/foo/www/rewrite.log" RewriteLogLevel 5 but the server configuration refuse it ... [Mon Nov 01 15:38:29 2010] [alert] [client 1.2.3.4] [host foo.com] /home/foo/www/.htaccess: RewriteLog not allowed here Sounds really cool :-) –  Warnaud Nov 1 '10 at 14:48
    
To be more clear, I have want to create an .htaccess that restrict access by IP to a folder (administration) and let my main index.php file handle the complete url if the folder/file exist. Example: foo.com/article/test should be handled by index.php (with kavoir method I pass 2 arguments to index.php: article and test) but that behaviour should not applied when typing foo.com/administration* I've tried to add RewriteCond %{REQUEST_FILENAME} !/administration/(.+) before the last rewrite rule but it doesn't work either ;( –  Warnaud Nov 2 '10 at 13:13

1 Answer 1

up vote 0 down vote accepted

I don't think the problem is in the SEO part.

Your first RewriteRule is broken. The syntax is:

RewriteRule pattern substitution [flags]

and I think that you want a second RewriteCond to check if the URI being requested has "administration" in it. So something like:

# Condition for the Rewriting rule: IP NOT starting with 1.2.3.4 (example)
RewriteCond %{REMOTE_ADDR} !^1\.2\.3\.4
# Trying to access administration pages.
RewriteCond %{REQUEST_URI} ^administration
# Redirect to the 404 page.
RewriteRule .+ http://localhost/404_page.html [R=404,L]
share|improve this answer
    
Hi dmah, thanks for answering. Using this rewriting rule and only this I have direct access to /administration :( Also people with the correct IP should be able to see inside the administration folder where url inside this folder are handled by /administration/index.php. –  Warnaud Nov 2 '10 at 15:31
    
You'll still need to add the second rewrite rule, i.e., the SEO part after this one. That will handle sending everything to index.php. The two RewriteCond's above should be AND'ed together before performing the rewrite. So if the IP address is not a "privileged" IP and the page being requested is in the "adminstration" hierarchy, the browser should get a 404. –  dmah Nov 2 '10 at 16:10
    
I added the SEO part, but even so it doesn't work I left IP 1.2.3.4 which I will have trouble to have :-) but I read directly the /index.php and only this one. I made a mistake in the SEO corrected on first post (the [OR]). Thanks a lot for the help –  Warnaud Nov 2 '10 at 17:31
    
In my second RewriteCond, it should be ^/administration watch the leading slash. I also think that the way the SEO part was written is correct and you've made it incorrect above. You don't need the [OR] and you should be check if !-f and !-d. Also, make sure that you are reloading your config each time you change it. –  dmah Nov 2 '10 at 18:45
    
Ultimate!! Thanks a lot dmah, I did some tweakings and added a new line to the SEO but it works now like a charm. Yes I need the -d or -f not the !-d !-f since theses folders and articles really exists at this particular place on the server. –  Warnaud Nov 2 '10 at 19:20

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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