Remove .php from URL

2020-02-10 04:14发布

Ubuntu 14.04LTS 32bit

LAMP

I know it's an old question but..

I need it to remove .php anywhere it finds it from the visible url. It needs to work with /showthread.php?id=XX ---> /showthread?id=XX

I can't even get it to work with /page.php --> /page. I've tried these:

Remove .php extension with .htaccess

How to hide the .html extension with Apache mod_rewrite

Remove .php from urls with htaccess

How to stop .htaccess loop

It just does nothing at all. While other .htaccess code works fine..

While

<?php 
phpinfo();

Lists mod_rewrite in Loaded Modules

And

<?php
 if(!function_exists('apache_get_modules') ){ phpinfo(); exit; }
 $res = 'Module Unavailable';
 if(in_array('mod_rewrite',apache_get_modules())) 
 $res = 'Module Available';
?>
<html>
<head>
<body>
<p><?php echo apache_get_version(),"</p><p>mod_rewrite $res"; ?></p>
</body>
</html>

Returns Module Available

Tried many more things

# 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>

#

RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.php [NC,L]

#

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

Not even this has any effect whatsoever:

RewriteRule ^page$ page.php [L]

sudo service apache2 restart does not change anything.

Server reboot changes nothing.

I tried clearing other code inside, did not make any change.

I cleared my browser cache 100 times

I'm starting to think that it just hates me. What could possible be causing this??

11条回答
做自己的国王
2楼-- · 2020-02-10 04:32

Hope helped.

It's worked for me.

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

# For LocalHost !.php
RewriteCond %{HTTP_HOST} !=localhost
RewriteCond %{HTTP_HOST} !=127.0.0.1
RewriteCond %{REMOTE_ADDR} !=127.0.0.1
RewriteCond %{REMOTE_ADDR} !=::1

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

# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]
查看更多
一夜七次
3楼-- · 2020-02-10 04:35
RewriteEngine on //replacement launch
RewriteCond %{REQUEST_FILENAME} !-d //If the requested object is not a folder
RewriteCond %{REQUEST_FILENAME}\.php -f //If the requested object to append the extension php - file
RewriteRule ^(.*)$ $1.php //make the change from concatenating .php
查看更多
倾城 Initia
4楼-- · 2020-02-10 04:39

Maybe this could be a duplicate but take a look at this:

How to remove .html from URL

Just in the solution change the hmtl for php

查看更多
▲ chillily
5楼-- · 2020-02-10 04:40

Check this link.

This is the answer using .htaccess:

RewriteEngine On

# browser requests PHP
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^\ ]+)\.php
RewriteRule ^/?(.*)\.php$ /$1 [L,R=301]

# check to see if the request is for a PHP file:
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^/?(.*)$ /$1.php [L]

Tested it on Windows with WAMP and working.

查看更多
别忘想泡老子
6楼-- · 2020-02-10 04:41

Assuming that .htaccess is being processed, then this super simple .htaccess should work for you:

RewriteEngine on
RewriteRule (.*) $1.php [END]

If it doesn't work, there is something wrong with your Apache configuration. You need to look into that first.

If it works, add the following line before the RewriteRule to allow serving other files:

RewriteCond %{REQUEST_FILENAME} !-f

The END flag is available since Apache 2.3.9.

查看更多
不美不萌又怎样
7楼-- · 2020-02-10 04:47

You may be on the right track. However, it sounds like your .htaccess file is not being executed. Just because a module is activated, does not mean it is available for you in your particular situation.

Here are some steps to solve your issue:

  • First of all, check the spelling very carefully. Verify that it is spelled correctly (including the . at the beginning)
  • Check the file permissions. Some servers are going to require executable permissions. So chmod the file to 755.
  • If you still do not have it working, go into your apache configuration (probably at /etc/apache2/apache2.conf on Ubuntu) and find every instance of AllowOverride. It might be set to 'none'. Change this to AllowOverride all instead.
  • Then go into sites-enabled, find your site configuration, and change the AllowOverride fields there are well.
  • Restart your Apache server and congratulate yourself with a big cup of coffee.

One of these should fix it. I would recommend trying between each step so that you can pinpoint where the error occurred. After determining the cause, you may want to go back and restrict some of those AllowOverrides, depending on your needs.

Best of luck!

查看更多
登录 后发表回答