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'm trying to do another SEO system with PHP/.htaccess...

I need the following rules to apply:

  1. Must catch all URLs that do not end with an extension (www.foo.com --> catch | www.foo.com/catch-me --> catch | www.foo.com/dont-catch.me --> don't catch).
  2. Must catch all URLs that end with .php* (.php, .php4...) (that are the exceptions to rule #1).
  3. All rules must only apply in some directories and not in their subdirectories (/ and /framework so far).
  4. The htaccess must send the typed URL in a GET value so I can work with it in PHP.

Any mod-rewrite wizard can help me?

Edit:

OK after reading the posts suggested, I came up with this but I think its stuck in a loop... Anyone can help?

<IfModule mod_rewrite.c>
RewriteEngine On
# 1
RewriteCond %{REQUEST_FILENAME} !\.[^/]+$ [OR]
# 2
RewriteCond %{REQUEST_FILENAME} \.php.*$
# 4
RewriteRule ^(.*)$ http://localhost/seo-urls/seo-urls-mapper.php%3Frequested=$1?%{QUERY_STRING}
</IfModule>

Since I want to complete this during my Xmas holidays, I'm adding a bounty :) Thanks for your help and happy holidays!

share|improve this question
2  
You may be best served by reading this question: Everything You Ever Wanted to Know about Mod_Rewrite Rules but Were Afraid to Ask? –  jscott Dec 21 '10 at 20:13

1 Answer 1

up vote 4 down vote accepted
+50

Try this as a starting point:

RewriteEngine On

RewriteCond %{QUERY_STRING} !^uri
RewriteCond %{REQUEST_URI}  !\.(.*)
RewriteCond %{REQUEST_URI}  !^/(framework|restricted)/
RewriteRule .* /index.php?uri=%{REQUEST_URI} [L,QSA]

RewriteCond %{QUERY_STRING} !^uri
RewriteCond %{REQUEST_URI}  \.php.?$
RewriteRule .* /index.php?uri=%{REQUEST_URI} [L,QSA]

First ruleset:

  1. Don't run this rule if the uri GET parameter is set (otherwise you'll get a loop when redirecting to the destination script after appending the uri parameter)
  2. If the URI contains a . then skip this ruleset (you may want to create a more specific rule for particular extensions but this works)
  3. If the URI is under a restricted directory, skip this ruleset
  4. Redirect to index.php (you may need to change the target) and set the uri GET parameter

Second ruleset:

  1. If the uri parameter is set, skip this ruleset to prevent a loop
  2. Proceed if the URI requested ends in .php, .php4, or similar (.php123 would not be redirected)
  3. Redirect to index.php (you may need to change the target) and set the uri GET parameter

It doesn't take a "wizard" to put mod_rewrite rules together - spend some time experimenting on your own and reading over the guide which jscott recommended.

Do ask all your theory-type questions here if you want great explanations (as opposed to practical-type questions in which it seems as though answerers are crowd-sourcing your project instead of furthering your knowledge).

share|improve this answer
    
Thanks for the tips, I will try them. Yeah I know it doesn't take a guru to make this work, but I'm a developer not a server guy and I (very) rarely work on this kind of stuff. Usually I lost what I learn due to lack of practice :) –  AlexV Dec 24 '10 at 14:23

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.