简体   繁体   English

htaccess 和 CMS 的 URL 问题

[英]URL problems with htaccess and CMS

I've developed my own CMS in PHP and now I'd like to make it SEO-Friendly, but I'm having some problems.我已经用 PHP 开发了我自己的 CMS,现在我想让它对 SEO 友好,但是我遇到了一些问题。

Most of the pages of the website are accessible from a id, like:网站的大部分页面都可以通过 id 访问,例如:

www.example.com/?id=alphanumericId123
www.example.com/?id=main

and so on, and I'd like to rewrite the URL of this pages:等等,我想重写这个页面的 URL:

www.example.com/aplhanumericId/
www.example.com/main/

I have wrote this rewrite rule, and it works fine:我写了这个重写规则,它工作正常:

RewriteEngine On
RewriteRule ^([^/d]+)/?$ index.php?id=$1 [QSA]

but It cannot work for those few pages that are accessible by a static file or just for the scripts.但它不能用于静态文件或仅用于脚本可访问的少数页面。
Isn't there a way to make some exceptions?没有办法做一些例外吗? Like:喜欢:

if the requested file exsist {
    if it is protected {
        display error 403
    } else {
        display file
} } else {
    rewrite rule
}

You should not rewrite paths to existing files and directories:您不应重写现有文件和目录的路径:

RewriteEngine On
# Exclude existing files
RewriteCond %{REQUEST_FILENAME} !-f
# Exclude existing directories
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/d]+)/?$ index.php?id=$1 [QSA]

Also note that your rule will not allow you alphanumeric id's as you are excluding numbers.另请注意,您的规则不允许您使用字母数字 ID,因为您排除了数字。

Alternatives would be:替代方案是:

...
# No forward slash
RewriteRule ^([^\/]+)/?$ index.php?id=$1 [QSA]

or或者

...
# Only a selection of characters including - and _
RewriteRule ^([-\w]+)/?$ index.php?id=$1 [QSA]

Use the following before your RewriteRule在 RewriteRule 之前使用以下内容

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

Basically it checks if it's not a file, and not a directory.基本上它会检查它是否不是文件,而不是目录。

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

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