如何让htacess区分文件和ID
问题描述:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule^https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d # not an existing dir
RewriteCond %{REQUEST_FILENAME} !-f # not an existing file
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}\.php -f # and page.php exists
# redirect to the physical page
RewriteRule ^(.*)$ $1.php [L]
# otherwise, redirect to serve.php
RewriteRule^/serve.php [L]
RewriteRule ^(\w+)$ ./serve.php?id=$1
因此,在代码的第一部分,我只需将http转换为https即可。我的目标是,我可以使用没有.php
的网址,但在我的旧代码中,我遇到了如果我这样做的问题,它被用作我的serve.php页面的ID。所以,如果使用https://example.com/contact
它就像https://example.com/serve.php?id=contact
,但我希望它的工作为https://example.com/contact.php
,但另一方面,我希望不是路线或文件的ID仍应作为IDS工作。我的旧代码是...如何让htacess区分文件和ID
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule^https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(\w+)$ ./serve.php?id=$1
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
答
您可以在网站根目录的.htaccess使用这些规则:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule^https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
# skip rules below this for files and directories
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule^- [L]
# rewrite to the physical php page
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
# otherwise, redirect to serve.php
RewriteRule ^/?$ serve.php [L]
RewriteRule ^(\w+)/?$ serve.php?id=$1 [L,QSA]
上帝保佑你,终于有人谁不报道我要重复,而且有利于真实的。继续吧! –
我会在7分钟内给你一个徽章.. –