質問

I am having issues trying to set up a correct htaccess file.

What I basically want to do is to implement clean URL and hide the .php extension except for one file.

What I currently have set up is the following:

Options +FollowSymLinks

RewriteEngine On

RewriteRule ^recruit(.*)$ recruit.php?id=/$1 [QSA,L]

#RewriteRule ^(?!recruit)(.*)$ $1.php [NC,L]
  1. The first rule will take anything after 'recruit' and pass it as a get variable in the url

    www.example.com/recruitHI --> www.example.com/recruit.php?id=HI

  2. What the other rule needs to do is to append .php to anything else other than anything that starts with recruit.

    www.example.com/index --> will look for index.php

    www.example.com/contact --> will look for contact.php

    www.example.com/recruit --> Needs to be ignored because of the first rule

When I have the 2 rules on and start apache, I get an error saying my configuration is wrong. They both work individually though.

役に立ちましたか?

解決

You can use:

Options +FollowSymLinks

RewriteEngine On

RewriteRule ^recruit(.*)$ recruit.php?id=/$1 [QSA,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ /$1.php [L]

2nd rule will add .php extension only for URIs that are not file or directories and that are already valid php files.

他のヒント

Try adding a condition to the second rule so that it won't blindly append a php to the end of the URI:

RewriteRule ^recruit(.*)$ recruit.php?id=/$1 [QSA,L]

RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}\.php -f
RewriteRule ^(.*)$ $1.php [NC,L]

Specifying RewriteBase at the beginning. If everything is in the site root it would be

RewriteBase /

Otherwise all your Rules should begin with ^/

RewriteEngine On
RewriteBase /
RewriteRule ^recruit(.*)$ recruit.php?id=/$1 [QSA,L]
RewriteRule ^(.*)$ $1.php [NC,L]

Since your have the L The rules after recruit will only affect items that don't have it. But instead of having a script for every url possibility, you should look at using a single Front Controller.

RewriteEngine On
RewriteBase /
RewriteRule ^recruit(.*)$ recruit.php?id=/$1 [QSA,L]
RewriteRule ^(.*)$ index.php [NC,L]

Then you can use FallBack provider instead (newer in Apache)

RewriteEngine On
RewriteBase /
RewriteRule ^recruit(.*)$ recruit.php?id=/$1 [QSA,L]

FallbackResource /index.php
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top