Using .htaccess to remove PHP file extension from URL

后端 未结 6 1545
名媛妹妹
名媛妹妹 2021-01-19 02:51

I just finished installing a LAMP stack on Ubuntu 12, and have run into an issue with Apache\'s .htaccess file. I have the rewrite and redirect mods enabled, and the .htacce

相关标签:
6条回答
  • 2021-01-19 03:17
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}\.php -f
    RewriteRule ^([^\.]+)/$ $1.php 
    
    0 讨论(0)
  • 2021-01-19 03:18
    # Apache Rewrite Rules
     <IfModule mod_rewrite.c>
      Options +FollowSymLinks
      RewriteEngine On
      RewriteBase /
    
    # Add trailing slash to url
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/|#(.*))$
      RewriteRule ^(.*)$ $1/ [R=301,L]
    
    # Remove .php-extension from url
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteCond %{REQUEST_FILENAME}\.php -f
      RewriteRule ^([^\.]+)/$ $1.php 
    
    # End of Apache Rewrite Rules
     </IfModule>
    
    0 讨论(0)
  • 2021-01-19 03:27

    You don't use htaccess to do this, you use your app to remove the extensions, and htaccess to map extension-less urls to real files. This rule

    # Remove file extension
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule (.*) $1.php [L]
    

    Says, "if the requested resource doesn't exist as a file, look for the resource with a .php extension". So you remove the extension from all links in your app, and this rule will make the php file run without the extension. Your htaccess is fine as-is, you need to update your app.

    0 讨论(0)
  • 2021-01-19 03:27

    Please try This code and tell me it performance.

      RewriteCond %{REQUEST_FILENAME} !-d
    
    RewriteCond %{REQUEST_FILENAME}.php -f
    RewriteRule ^(.*)/$ $1.php
    
    RewriteCond %{REQUEST_FILENAME}.html -f
    RewriteRule ^(.*)/$ $1.html
    
    RewriteCond %{REQUEST_FILENAME}.py -f
    RewriteRule ^(.*)/$ $1.py
    
    0 讨论(0)
  • 2021-01-19 03:35

    There is another htaccess alternative I use very successfully:

    Options +FollowSymLinks
    
    Options -Indexes
    
    RewriteEngine On
    
    RewriteRule ^purchase-jelly-babies$ /modules/products/jelly_babies.php [L]
    
    RewriteRule ^/lets/use/an/asp/extension.asp$ /modules/test/asp_example.php [L]
    

    This method not only solves your PHP extension issue but also allows you to keep your files organized no matter what those SEO idiots tell you what the URL should be.

    0 讨论(0)
  • 2021-01-19 03:36

    the way am doing it.

    RewriteEngine on
    RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
    RewriteRule ^(.+)\.php$ http://domainname.com/$1 [R=301,L]
    
    0 讨论(0)
提交回复
热议问题