简体   繁体   English

htaccess删除.php文件扩展名

[英]htaccess remove .php file extension

I am Using htaccess, i am trying to stop .php displaying on files in the address bar. 我正在使用htaccess,我试图停止在地址栏中文件上显示的.php。 i currently have this code: 我目前有此代码:

RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.php -f 
RewriteRule ^([a-zA-Z0-9-]+)$ index.php?pagename=$1.php

and index.php has 和index.php有

include $_GET["pagename"];

if the url is domain.com/customer/tickets/openticket 如果网址是domain.com/customer/tickets/openticket

it should rewrite to domain.com/customer/tickets/openticket.php 它应该重写为domain.com/customer/tickets/openticket.php

how can i make this apply only to 我怎样才能使这个仅适用于

domain.com/customer and my.domain.com ? domain.com/customer和my.domain.com?

First, check PHP extension URL_rewrite When below code find a openticket on your URL, rewrite it to openticket.php 首先,检查PHP扩展URL_rewrite当以下代码在您的URL上找到openticket时,将其重写为openticket.php

<IfModule mod_rewrite.c>
RewriteEngine on

RewriteRule     ^openticket/?$      openticket.php          [L,NC]


RewriteCond  %{REQUEST_FILENAME} -f
RewriteCond  %{REQUEST_FILENAME} -d
RewriteRule . /             [L,R=301]


</IfModule>

The url rewrite rules are not magic. url重写规则不是魔术。 You can not just write a system with a thousand pages and then use the rule to do what you want. 您不能只编写具有一千个页面的系统,然后使用该规则执行所需的操作。 You need to write the system already thinking about the rules. 您需要编写已经考虑规则的系统。 What can you do in this case is to rewrite all the rules saying that when you find certain name in the url, will draw particular file. 在这种情况下,您可以做的是重写所有规则,说当您在url中找到特定名称时,将绘制特定文件。 I do not particularly know magic to it. 我并不特别了解魔术。

It would be something like: 就像这样:

 RewriteRule      ^page1/?$     page1.php     [L,NC]
 RewriteRule      ^page2/?$     page2.php     [L,NC]
 RewriteRule      ^page3/?$     page3.php     [L,NC]
 RewriteRule      ^page4/?$     page4.php     [L,NC]

And etc.. 等等..
If any other user to know a magic to it, please let me know. 如果还有其他用户知道它的魔力,请告诉我。

Update: 更新:

Maybe you can try some like as this (it is just a idea/magic): 也许您可以尝试这样的操作(这只是一个想法/魔术):

 RewriteRule ^([a-zA-Z-]+)?$ /$1.php [NC]

Try: 尝试:

RewriteCond %{REQUEST_URI} ^/customer/ [OR]
RewriteCond %{HTTP_HOST} ^my\.domain\.com$ [NC]

RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}.php -f
RewriteRule ^(.*)$ index.php?pagename=$1.php [L]

That will only try to add the php extension when it's inside the "customer" folder or if the host is "my.domain.com". 只有在“ customer”文件夹内或主机为“ my.domain.com”时,才尝试添加php扩展名。 You can bypass the index.php file entirely by simply replacing the last line with: 您只需将最后一行替换为:即可完全绕开index.php文件:

RewriteRule ^(.*)$ /$1.php [L]

In order to remove the php extension when someone puts something like http://domain.com/customer/tickets/openticket.php in their browser's address bar, you'll need this: 为了在有人在其浏览器的地址栏中放入http://domain.com/customer/tickets/openticket.php类的内容时删除 php扩展名,您需要执行以下操作:

RewriteCond %{REQUEST_URI} ^/customer/ [OR]
RewriteCond %{HTTP_HOST} ^my\.domain\.com$ [NC]

RewriteCond %{THE_REQUEST} \ /+([^\?\ ]+)\.php
RewriteRule ^ /%1 [L,R=301]

try : 尝试:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteCond %{REQUEST_URI} !^(.*)/customer/(.*)
RewriteRule ^(.*)$ index.php?pagename=$1.php

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

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