关于php:使用.htaccess重写动态子域 | 珊瑚贝

Using .htaccess to rewrite dynamic subdomains


我目前正在使用 .htaccess 在我的网站上重写以创建动态子域。我还使用它来删除所有页面上的 .php 扩展名。但是,一旦进入子域,它就会再次尝试重定向。例如,如果您访问 https://admin.example.com/test,它实际上将访问 https://example.com/clients/admin/test.php。我使用以下 .htaccess 文件不断收到各种 404 错误:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Options +MultiViews
Options +FollowSymLinks

RewriteEngine On
RewriteRule ^subdomains/(.*)/(.*) http://$1.example.com/$2 [r=301,nc]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\\.]+)$ $1.php [NC,L]

RewriteCond %{HTTP_HOST} !^(www\\.)?example\\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\\.)?([^\\.]+)\\.example\\.com$ [NC]
RewriteCond %{DOCUMENT_ROOT}/%2%{REQUEST_URI}/ d
RewriteRule [^/]$ %{REQUEST_URI}/ [R=301,L]

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} !^(www\\.)?example\\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\\.)?([^\\.]+)\\.example\\.com$ [NC]

RewriteRule ^(.*)$ clients/%2/$1 [QSA,L]

如何防止它重定向到 https://admin.example.com/clients/admin/test.php?


首先,您可能想关闭多视图,这会弄乱整个”删除 php 扩展”的事情

比,你想要这条规则:

1
2
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\\.]+)$ $1.php [NC,L]

在您的重定向之后,并且您希望对实际重定向进行重定向状态检查:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Options Multiviews + FollowSymLinks

RewriteEngine On

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^subdomains/(.*)/(.*) http://$1.example.com/$2 [r=301,nc]

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} !^(www\\.)?example\\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\\.)?([^\\.]+)\\.example\\.com$ [NC]
RewriteCond %{DOCUMENT_ROOT}/%2%{REQUEST_URI}/ d
RewriteRule [^/]$ %{REQUEST_URI}/ [R=301,L]

RewriteCond %{REQUEST_URI} !^/clients/
RewriteCond %{HTTP_HOST} !^(www\\.)?example\\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\\.)?([^\\.]+)\\.example\\.com$ [NC]
RewriteRule ^(.*)$ clients/%2/$1 [QSA,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php f
RewriteRule ^([^\\.]+)$ $1.php [NC,L]

  • 使用子域规则时,这给了我 500 错误。
  • @brokekidweb 抱歉错过了一个条件


来源:https://www.codenong.com/19850995/

微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_8902.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?