0

我有一家网店,想使用 htaccess 缩短链接,有 3 种 url 情况:

shop.com/shop/18 (number) - products.php?categoryid=$1
shop.com/shop/18/page-2 (number)/(page+number) - products.php?categoryid=$1&page=$2
shop.com/shop/18/9877 (number)/(number) - description?categoryid=$1&productid=$2

我的尝试

RewriteRule ^shop/?$                        shop.php
RewriteRule ^shop/(.*)/([0-9]+)/?$          description.php?categoryid=$1&productid=$2
RewriteRule ^shop/(.*)/page-(.*)/?$         products.php?categoryid=$1&page=$2
RewriteRule ^shop/(.*)/?$                   products.php?categoryid=$1

用我的尝试 - 1(有效),2(有效),3(无效)

  1. 我怎样才能重写网址呢?

  2. 如果没有这样数量的类别或这样的产品(猜测用 php 和 mysql 检查然后重定向),我如何重定向到 404 页面?

4

2 回答 2

2

有很多方法可以解决这个问题;

  • 全部在 htaccess 中(因多个深度而变得混乱)
  • 结合 htaccess 和服务器端代码

最好的方法是根据商店的编码方式适合您的方法。我个人认为在服务器端代码中处理它更好,它简化了 htaccess 文件,并且在验证数据、如何处理发送的内容、发送到哪里以及发送到那里时如何处理它方面为您提供了更多控制权.

例如,在我的 htaccess 文件中;

<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine on
#
# Do not apply rewrite rules for non required areas 
RewriteCond %{REQUEST_URI} "/hidden-areas/" [OR]
RewriteCond %{REQUEST_URI} "/other-areas/"
RewriteRule (.*) $1 [L]

# Do Not apply if a specific file or folder exists
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# The rules on how to rewrite the urls  
RewriteRule (.*) /index.php?url=$1 [QSA,L]
</IfModule>

基本上,简而言之,我不会为某些文件夹重写任何内容,我会直接转发它们。这是为了停止对外部脚本的调用,或者可以毫无问题地访问额外添加的系统。

然后,我将整个 url 作为字符串转发到我的索引页面,并处理使用 PHP 产生的内容,下面是一个示例。

// collect the passed url
$url = $_GET['url'];
// split the url into parts
$url_parts = explode('/', $url);
/*
* start sorting what is what in the url
*/
// count how many parts there are
$url_parts_count = count($url_parts);
// determine the class/module
$class = $url_parts[0]; // generally the class/method/module depending on your system, thgough could be a category so run some checks
// determine the last part in the array
$last_url_part = ($url_parts_count - 1);
// set the last part of the url to be used
$slug = $url_parts[$last_url_part]; // generally the slug and will be empty if theres a trailing slash
etc etc etc

这只是一个总结,我做的更多,因为这是从我写的一个 CMS 中摘录的,但如果你希望自己动手,它应该会给你一个很好的起点。当然,如有必要,我很乐意进一步详细说明。

当然,需要注意的是,如果您使用的是现成的系统,他们应该已经为您提供了此代码;)

我根据您更新的问题在下面添加了一些内容,如果您仍然打算按照自己的方式行事,这将有所帮助:)

<IfModule mod_rewrite.c>
  Options +FollowSymlinks
  RewriteEngine on
  RewriteBase /
  #
  # Do not apply rewrite rules for non required areas 
    RewriteCond %{REQUEST_URI} "/hidden-areas/" [OR]
    RewriteCond %{REQUEST_URI} "/other-areas/"
    RewriteRule (.*) $1 [L]

  # Do Not apply if a specific file or folder exists
  # RewriteCond %{REQUEST_FILENAME} !-f
  # RewriteCond %{REQUEST_FILENAME} !-d
  # The rules on how to rewrite the urls

  RewriteRule ^([a-zA-Z0-9_-]+)$ /index.php?slug=$1 [QSA,L]  
  RewriteRule ^([a-zA-Z0-9_-]+)/$ /index.php?type=$1 [QSA,L]  
  RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ /index.php?type=$1&slug=$2 [QSA,L]  
  RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/$ /index.php?type=$1&cat=$2 [QSA,L]
  RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ /index.php?type=$1&cat=$2&slug=$3 [QSA,L] 
</IfModule>
于 2013-02-20T13:25:42.670 回答
0

谢谢,结束了

RewriteRule ^shop$                             shop.php [L]
RewriteRule ^shop/([0-9]+)$                    products.php?categoryid=$1 [L]
RewriteRule ^shop/([0-9]+)/(page-[0-9]+)$      products.php?categoryid=$1&page=$2 [L]
RewriteRule ^shop/([0-9]+)/([0-9]+)$           description.php?categoryid=$1&productid=$2 [L]
于 2013-02-20T13:51:29.160 回答