如何删除和重定向.htaccess中子域的.php


How to remove and redirect .php for subdomains in .htaccess?

我有以下代码来删除.php扩展名,并将不带后缀的url重定向到正确的文件

# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.+)'.php([#?][^' ]*)?' HTTP/
RewriteRule ^(.+)'.php$ http://www.example.com/$1 [R=301,L]
# Resolve .php file for extensionless php urls
RewriteRule ^([^/.]+)$ $1.php [L]

我还有以下代码可以将m.example.com重定向到www.example.com/m/

#mobile
RewriteCond %{HTTP_HOST} ^m'.example'.com$ [NC]
RewriteCond %{REQUEST_URI} !^/m(/|$) [NC]
RewriteRule ^(.*)$ m/$1 [L]

我的问题是,我应该如何对m文件夹中的文件进行同样的处理,以便

  1. 当人们输入m.example.com/test时,它将转到m.example.com/test.php
  2. 当人们访问m.example.com/test.php时,会显示该文件,但url随后会更改为m.example.com/test

谢谢。

组合您的规则并从重定向规则的目标中删除http://www.example.com/

# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} 's/+(.*?/)?(?:index)?(.*?)'.php['s?] [NC]
RewriteRule ^ /%1%2 [R=302,L,NE]
#mobile
RewriteCond %{HTTP_HOST} ^m'.example'.com$ [NC]
RewriteCond %{REQUEST_URI} !^/m(/|$) [NC]
RewriteRule ^(.*)$ m/$1 [L]
# Resolve .php file for extension-less php urls
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1'.php -f [NC]
RewriteRule ^(.+?)/?$ /$1.php [L]