I already asked a question about redirecting users with a certain IP 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]
Best Answer
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: