使用.htaccess文件删除.php文件扩展名

时间:2023-02-08 07:17:21

I want www.example.com/about.php to just be www.example.com/about

我想www.example.com/about.php只是www.example.com/about

I created an .htaccess file and placed it in the root of my server. I am using linux shared hosting. PHP version 5.2

我创建了一个.htaccess文件并将其放在我的服务器的根目录中。我正在使用linux共享主机。 PHP版本5.2

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

It seems that this code should work properly, but for some reason it does not. I get a 404 error when I try to navigate to a page without the .php extension.

似乎这段代码应该正常工作,但由于某些原因它不能。当我尝试导航到没有.php扩展名的页面时出现404错误。

Here's my markup too:

这也是我的标记:

<nav>
        <div class="container">
            <ul>
                <li><a href="index.php" <?php if (strpos($_SERVER['PHP_SELF'], 'index.php')) echo 'class="current-nav"';?>>home</a></li>
                <li><a href="about.php">about</a></li>
                <li><a href="services">our services</a></li>
                <li><a href="portfolio" <?php if (strpos($_SERVER['PHP_SELF'], 'portfolio.php')) echo 'class="current-nav"';?>>portfolio</a></li>
                <li><a href="testimonials" <?php if (strpos($_SERVER['PHP_SELF'], 'testimonials.php')) echo 'class="current-nav"';?>>testimonials</a></li>
                <!--<li><a href="#">client area</a></li>-->
                <li><a href="contact" <?php if (strpos($_SERVER['PHP_SELF'], 'contact.php')) echo 'class="current-nav"';?>>Contact</a></li>
                <li><a href="order" <?php if (strpos($_SERVER['PHP_SELF'], 'order.php')) echo 'class="current-nav"';?>><strong>Free Quote</strong></a></li>
            </ul>
        </div>
    </nav><!--/navigation--> 

You can see I tried using the php extension in the link and also tried without. The links with the php extension go to the page, but don't remove the extension.

你可以看到我尝试在链接中使用php扩展,并尝试没有。与php扩展的链接转到页面,但不删除扩展名。

I did a test to see if module_rewrite was enabled by putting in some garbage and returning a 500 error.

我做了一个测试,看看是否通过输入一些垃圾并返回500错误来启用module_rewrite。

5 个解决方案

#1


37  

Use this code for hiding/removing .php extension:

使用此代码隐藏/删除.php扩展名:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]

## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]

#2


7  

So after a long bout with google I finally figured this one out. This works with Godaddy shared hosting. It removes php file extensions so http://yoursite.com/about.php becomes http://yoursite.com/about

所以经过与谷歌的长时间回合后,我终于想出了这个。这适用于Godaddy共享主机。它删除了php文件扩展名,因此http://yoursite.com/about.php成为http://yoursite.com/about

Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f    #If requested is not a filename...
RewriteCond %{REQUEST_FILENAME} !-d    #And not a directory
RewriteRule ^(.*)$ $1.php [L]          # perform this redirect

(remove comments before uploading to server)

(在上传到服务器之前删除评论)

#3


4  

I had this problem also, but I found that this seemed to fix the GoDaddy .htaccess problem.

我也有这个问题,但我发现这似乎解决了GoDaddy .htaccess问题。

# Fix Rewrite
Options -Multiviews

# Mod Rewrite
Options +FollowSymLinks
RewriteEngine On
RewriteBase / 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.php -f 
RewriteRule ^(.*)$ $1.php 

#4


1  

Pasted above everything in my .htaccess file worked for me...

粘贴在我的.htaccess文件中的所有内容为我工作...

## 301 Redirects
RewriteCond %{QUERY_STRING}  ^$
RewriteRule ^(.*)\.asp$ $1? [R=301,NE,NC,L]

#5


0  

Your rewrite rule isn't correct, it's the wrong way round. Try this instead:

你的重写规则是不正确的,这是错误的方式。试试这个:

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

#1


37  

Use this code for hiding/removing .php extension:

使用此代码隐藏/删除.php扩展名:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]

## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]

#2


7  

So after a long bout with google I finally figured this one out. This works with Godaddy shared hosting. It removes php file extensions so http://yoursite.com/about.php becomes http://yoursite.com/about

所以经过与谷歌的长时间回合后,我终于想出了这个。这适用于Godaddy共享主机。它删除了php文件扩展名,因此http://yoursite.com/about.php成为http://yoursite.com/about

Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f    #If requested is not a filename...
RewriteCond %{REQUEST_FILENAME} !-d    #And not a directory
RewriteRule ^(.*)$ $1.php [L]          # perform this redirect

(remove comments before uploading to server)

(在上传到服务器之前删除评论)

#3


4  

I had this problem also, but I found that this seemed to fix the GoDaddy .htaccess problem.

我也有这个问题,但我发现这似乎解决了GoDaddy .htaccess问题。

# Fix Rewrite
Options -Multiviews

# Mod Rewrite
Options +FollowSymLinks
RewriteEngine On
RewriteBase / 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.php -f 
RewriteRule ^(.*)$ $1.php 

#4


1  

Pasted above everything in my .htaccess file worked for me...

粘贴在我的.htaccess文件中的所有内容为我工作...

## 301 Redirects
RewriteCond %{QUERY_STRING}  ^$
RewriteRule ^(.*)\.asp$ $1? [R=301,NE,NC,L]

#5


0  

Your rewrite rule isn't correct, it's the wrong way round. Try this instead:

你的重写规则是不正确的,这是错误的方式。试试这个:

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