Remove PHP extension using .htaccess methods (Keeping Link Juice)

淺唱寂寞╮ 提交于 2019-12-11 12:07:45

问题


I'm trying to remove the .php extension on my URLs to make it Search Engine Friendly using the .htaccess file Redirect 301, to keep the "rank juice" and I as much I've tried almost every example around - It just doesn't seem to work.

Here are some of the methods I've unsuccessfully tried already:

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

This is the most common solution given but nothing happens and there is no changes.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /$1.php [L]

Nothing happens and there is no changes.

RewriteEngine On
# RewriteCond %{THE_REQUEST} ^GET\ /[^?\s]+\.php
# RewriteRule (.*)\.php$ /$1 [L,R=301]
# RewriteRule (.*)/$ $1.php [L]

I get a Not Found : The requested URL /AMENPTHOME/hostnd/3/9/9/399fc7b78/www/htdocs/web/dive-sites.php was not found on this server.

Amen is my host and the file dive-sites.php in this specific real example is on the root. My goal is to have: www.domain.com/dive-sites and not www.domain.com/dive-sites.php using a Redirect 301 because this url is already ranked for a while. Can someone please help ?

Thank you very much, all help appreciated.


回答1:


If I didn't missed anything:

RewriteEngine On

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/(.+)\.php[^\s]* [NC]
RewriteRule ^ /%1 [R=301,NE,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [QSA,NC,L]

Requests to /dive-sites.php will issue a 301 redirect to /dive-sites, appending query-strings if any.

Requests to /dive-sites will get a 200 response with /dive-sites.php as Content-Location, appending query-strings if any.




回答2:


try this:

RewriteEngine on

RewriteRule ^/(.+)(($|#)*(.*)) /$1.php$2

If removing only the .php this will work.
$1 = for the name of the page, e.g foo
$2 = take note of the hashtag or the get request

eg.
<domain>/example ----> <domain>/example.php
<domain>/example?get=this ----> <domain>/example.php?get=this
<domain>/example#hash ----> <domain>/example.php#hash




回答3:


Try this code for .php extension removal:

RewriteEngine On

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

# To internally forward /dir/file to /dir/file.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ /$1.php [L]


来源:https://stackoverflow.com/questions/19904911/remove-php-extension-using-htaccess-methods-keeping-link-juice

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!