简体   繁体   中英

Silently Add File Extension in Subdirectory w/ htaccess

I recently moved dev sites and on each dev site the project was in a subdirectory (ie: www.site.com/dev_site). When I switched to a new dev site (ie: www.site2.com/dev_site), none of the links on the site would work properly unless they included the .php extension. So say a link like www.site2.com/account would throw a 404 error with:

The requested URL /dev_site/account was not found on this server

The previous htaccess file on the old dev site that worked fine was silently adding .php to the end of every uri and not displaying it in the url.

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}.php -f
    RewriteRule ^(.*)$ $1.php [L,QSA]
</IfModule>

I know that mod_rewrite is on because I can put garbage in the file and it throws a 500.

I have also tried:

<IfModule mod_rewrite.c>
    Options All -Indexes -MultiViews
    RewriteEngine on
    RewriteBase /dev_site/

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

And all that did was throw a 500 error. And removing Options All -Indexes -MultiViews would just bring me back to the 404 not found error mentioned previously.

I was able to fix this from an answer I found here: https://stackoverflow.com/a/30487642/2122300

I have altered it to look like:

<IfModule mod_rewrite.c>
    Options +FollowSymLinks -MultiViews

    RewriteEngine On
    RewriteBase /dev_site/

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule (.*) /dev_site/$1.php [L]
</IfModule>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM