简体   繁体   English

如何编辑.htaccess删除.php

[英]How to edit .htaccess to remove .php

All of my pages end in .php, I need these pages to still be able to run PHP even though the extension has changed in the url bar. 我所有的页面均以.php结尾,即使URL栏中的扩展名已更改,我仍需要这些页面才能运行PHP。 I want a page like 我想要一个类似的页面

website.com/page.php

To

website.com/page

I have looked at half a dozen work arounds to this problem but none of them seem to work. 我研究了六种解决此问题的方法,但是似乎都没有。 Here are some examples. 这里有些例子。

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [L, QSA]

RewriteEngine On
RewriteRule \/([^\/]+)\/$ $1.php

# once per htaccess file
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z0-9-_]+)/?$ /$1.php
RewriteCond %{REQUEST_URI} !^/shopName1.php$
RewriteRule ^([a-z0-9-_]+).php$ /$1/ [R]
RewriteRule ^shops/shopName1/?$ /shopName1.php
RewriteRule ^shopName1.php$ /shops/shopName1/ [R]

Hide the .php Extension 隐藏.php扩展名

RewriteEngine On
RewriteCond /%{REQUEST_FILENAME}.php -f
RewriteRule ^([a-zA-Z0-9_-\s]+)/$ /$1.php

Explanation How this work 说明如何运作

Ans. 答。 This rule will match url.com/path/ and check, if url.com/path.php exists. 此规则将匹配url.com/path/并检查url.com/path.php是否存在。 If file exists then process ahead to rewrite rule apply. 如果文件存在,则需要执行重写规则。

Currently I used this rule Work perfect, Hope this help you! 目前,我已使用此规则完美工作,希望对您有所帮助!


Updated 更新

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

in the above code the -d define for Directory and -f for Regular File. 在上面的代码中, -d定义目录, -f定义常规文件。

This rule will same as above but its support Alphanumeric URL . 该规则与上述相同,但支持字母数字URL The above code is Tested and Work Fine with my server files. 上面的代码已经过测试,并且可以与我的服务器文件正常工作。

Example: If your filename is "www.url.com/path1.php" then your may access this file directly with "www.url.com/path1" . 示例:如果您的文件名是“ www.url.com/path1.php”,那么您可以直接通过“ www.url.com/path1”访问该文件。

尝试这个

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f

RewriteRule ^([^\\.]+)$ $1.php [NC,L]

Just make a directory called page and rename page.php as index.php and put in in /page. 只需建立一个名为page的目录,并将page.php重命名为index.php并放入/ page中。 If you need dynamic directories (like /product5 runs details.php?productid=5), then an .htaccess solution would be useful. 如果您需要动态目录(例如/ product5运行details.php?productid = 5), 那么 .htaccess解决方案将非常有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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