简体   繁体   English

使用 PHP 和 .htaccess (mod_rewrite) 管理 slug

[英]Manage slugs with PHP and .htaccess (mod_rewrite)

Let's call my site:让我们调用我的网站:

www.example.com

and I have a PHP file like this:我有一个像这样的 PHP 文件:

www.example.com/product.php?id=50

I would like to access it by using我想通过使用访问它

www.example.com/product/50

but ALSO, very important, I have several subdirectories like但同样,非常重要,我有几个子目录,比如

www.example.com/subsite/product.php?id=50
www.example.com/subsubsite/product.php?id=50

That must become那必须变成

www.example.com/subsite/product/50
www.example.com/subsubsite/product/50

How can I solve it at best with PHP and .htaccess using mod_rewrite?如何使用 mod_rewrite 使用 PHP 和.htaccess最好地解决它? I banged my head with other questions like this one but to no avail.我用其他类似问题敲了敲头,但无济于事。

I can't seem to find a solution that works flawlessly, taking care of all imported files like CSS, JS and PHP classes.我似乎找不到完美运行的解决方案,处理所有导入的文件,如 CSS、JS 和 PHP 类。

Ok so this might not be the complete answer but should help you find your way.好的,所以这可能不是完整的答案,但应该可以帮助您找到自己的方式。

You can use regex to match your desired path pattern.您可以使用正则表达式来匹配您想要的路径模式。 So for example your htaccess might look something like...因此,例如,您的 htaccess 可能看起来像...

# Check if module is installed
<IfModule mod_rewrite.c>

RewriteEngine On
RewriteBase /

# Check query for matching pattern and pass id, but also append additional query strings
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\/]+\/)?product\/([0-9]+)$ /$1product.php?id=$2 [L,QSA]

# If not file or directory on server, send to 404.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /404.php [L]

</IfModule>

And what this does is...而它的作用是......

1. Match the uri with a regex pattern 1.将uri与正则表达式匹配

Regex: ^([^\/]+\/)?product\/([0-9]+)$正则表达式: ^([^\/]+\/)?product\/([0-9]+)$

  1. ^ - Start of string. ^ - 字符串的开始。
  2. ([^\/]+\/)? - matches any directory (if exists) and stores it for reuse. - 匹配任何目录(如果存在)并将其存储以供重用。
  3. product\/([0-9]+) - Your desired path eg product/50 and stores the number "id" for reuse. product\/([0-9]+) - 您想要的路径,例如product/50并存储数字“id”以供重复使用。
  4. $ - End of string. $ - 字符串结束。

2. Pass captured directory and id to our file 2. 将捕获的目录和 id 传递给我们的文件

Like so: /$1product.php?id=$2 [L,QSA]像这样:/ /$1product.php?id=$2 [L,QSA]

  1. $1 is our directory name including the trailing slash eg subsubsite/ $1是我们的目录名称,包括尾部斜杠,例如subsubsite/
  2. $2 is our product id eg 50 $2是我们的产品编号,例如50
  3. [L,QSA] The QSA flag means we can access additional query string parameters eg /product/50?show=all&updated=1 . [L,QSA] QSA 标志意味着我们可以访问其他查询字符串参数,例如/product/50?show=all&updated=1 More about flags can be found here http://httpd.apache.org/docs/current/rewrite/flags.html#flag_qsa更多关于标志的信息可以在这里找到http://httpd.apache.org/docs/current/rewrite/flags.html#flag_qsa

3. 404 anything not matching 3. 404 不匹配

Like so:像这样:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /404.php [L]
  1. !-f If request is not a file !-f如果请求不是文件
  2. !-d If request is not a directory !-d如果请求不是目录
  3. /404.php The file used for presenting a 404 error. /404.php用于显示 404 错误的文件。

Getting the id...拿到身份证...

With the above, you can get the ID within your product.php file like so:通过以上,您可以在product.php文件中获取 ID,如下所示:

$id = (int)$_GET[ 'id' ];

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

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