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.

What i want:

  • Force www [works]
  • Restrict access to .inc.php [works]
  • Force redirection of abc.php to /abc/
  • Removal of extension from url
  • Add a trailing slash if needed

old .htaccess :

    Options +FollowSymLinks


    <IfModule mod_rewrite.c>

    RewriteEngine On
    RewriteBase /

    ### Force www
    RewriteCond %{HTTP_HOST} ^example\.net$
    RewriteRule ^(.*)$ http://www\.example\.net/$1 [L,R=301]


    ### Restrict access
    RewriteCond %{REQUEST_URI} ^/(.*)\.inc\.php$ [NC]
    RewriteRule .* - [F,L]


    #### Remove extension:
    RewriteRule ^(.*)/$ /$1.php [L,R=301]



    ######### Trailing slash:
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_URI} !(.*)/$
    RewriteRule ^(.*)$ http://www.example.net/$1/ [R=301,L]
    </IfModule>

New .htaccess:

     Options +FollowSymLinks


     <IfModule mod_rewrite.c>

    RewriteEngine On
    RewriteBase /

    ### Force www
    RewriteCond %{HTTP_HOST} ^example\.net$
    RewriteRule ^(.*)$ http://www\.example\.net/$1 [L,R=301]


    ### Restrict access
    RewriteCond %{REQUEST_URI} ^/(.*)\.inc\.php$ [NC]
    RewriteRule .* - [F,L]


    #### Remove extension:
    RewriteCond %{REQUEST_FILENAME} \.php$
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule (.*)\.php$ /$1/ [L,R=301]

    #### Map pseudo-directory to PHP file
    RewriteCond %{REQUEST_FILENAME}\.php -f
    RewriteRule (.*) /$1.php [L]

    ######### Trailing slash:
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteCond %{REQUEST_FILENAME} !/$
    RewriteRule (.*) $1/ [L,R=301]

    </IfModule>

errorlog:

     Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace., referer: http://www.example.net/

Rewrite.log: http://pastebin.com/x5PKeJHB

share|improve this question

marked as duplicate by Jenny D, Tim Brigham, Rex, Ward, TheCleaner Apr 8 at 12:55

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

2 Answers 2

You are catching the trailing slash you added in the "remove extension" block in the next block, which breaks the call to the local file, and will keep tacking on .php's since the "trailing slash" block will never match so it wraps back around to the "Map psudo-dir" block.

Add '(/?)' backreference, do you don't accidentally have "/" in your $1 #### Map pseudo-directory to PHP file RewriteCond %{REQUEST_FILENAME}.php -f RewriteRule /(.*)(/?) /$1.php [L]

and, the "-d" should be a "-f" from what i can tell:

### Trailing slash:
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} !/$
RewriteRule (.*) $1/ [L,R=301]

Though This seems somewhat convoluted. You should just have your site link to the "/abc/" dir instead of directly to the php files, and redirect those.

share|improve this answer

This seems to be your initial problem:

#### Map pseudo-directory to PHP file
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule (.*) /$1.php [L]

You're trapping a '/' character.

119.154.34.231 - - [03/Oct/2010:16:17:06 +0000] [www.example.net/sid#7f3fa2d89670][rid#7f3fa3466b58/initial] (4) [perdir /var/www/example.net/public_html/] RewriteCond: input='/var/www/example.net/public_html/how-works.php' pattern='-f' => matched

119.154.34.231 - - [03/Oct/2010:16:17:06 +0000] [www.example.net/sid#7f3fa2d89670][rid#7f3fa3466b58/initial] (2) [perdir /var/www/example.net/public_html/] rewrite 'how-works/' -> '/how-works/.php'

Try:

RewriteRule ^/(.*)/(.?) /$1.php [L]

...I might have that a bit wrong, but that seems like where you're first going wrong.

share|improve this answer
    
now i get a 404 : File does not exist: /var/www/example.net/public_html/how-works –  Shoaibi Oct 3 '10 at 20:27
    
remove the / of /$1.php you don't need to define root when you are already at root –  Prix Oct 3 '10 at 21:05
    
same as before, file does not exist, do you want to paste rewrite log? –  Shoaibi Oct 3 '10 at 21:23
    
Rewrite log: pastebin.com/0SMqSkaU after removing "/" from /$.php on RewriteRule –  Shoaibi Oct 3 '10 at 21:26

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