【问题标题】:htaccess Mod Rewrite rule for multiple URLs and PHP $_GET's多个 URL 和 PHP $_GET 的 htaccess Mod 重写规则
【发布时间】:2013-12-24 23:03:11
【问题描述】:

我的 htaccess 文件中有这些 mod 重写规则:

RewriteEngine On

# Displays directory if there is no / on the end of the URL
RewriteCond %{REQUEST_URI} !^/admin [NC]
RewriteCond %{REQUEST_URI} !^/status [NC]
RewriteCond %{REQUEST_URI} !^/customer [NC]

# Removes index.php from URL 
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(.*?)index\.php$ /$1 [L,R=302,NC,NE]

# Rewrites /services to be /index.php?id=services
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9-]+)/?$ /index.php?id=$1 [L,QSA]

# Rewrites /blog/this-is-a-blog-post to be /index.php?id=blog&slug=this-is-a-blog-post
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9-]+)/([\w-]+)/?$ /index.php?id=$1&slug=$2 [L,QSA]

# Rewrites /blog/year2013/month12 to be /index.php?id=blog&year=2013&month=01
RewriteRule ^([a-zA-Z0-9-]+)/year([0-9]+)/month([0-9]+)/?$ /index.php?id=$1&year=$2&month=$3 [L,QSA]

# Rewrites /status/123 to be /index.php?id=status&seq=123
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9-]+)/([\w-]+)/?$ /index.php?id=$1&seq=$2 [L,QSA]

# Rewrites /status/cat1 to be /index.php?id=status&cat=1
RewriteRule ^([a-zA-Z0-9-]+)/cat([0-9]+)?$ /index.php?id=$1&cat=$2 [L,QSA]

/blog 工作正常,但 /status 不行。它显示即使没有目录的目录索引

我基本上想要:

  1. home.php?id=services to look like domain.com/services(此为 多个链接/网址)- 工作正常
  2. home.php?id=blog&year=2013&month=12 看起来像 domain.com/blog/year2013/month12 工作正常
  3. home.php?id=blog&slug=this-is-a-blog-post 看起来像 domain.com/blog/this-is-a-blog-post 工作正常
  4. home.php?id=status&cat=123 变为 domain.com/status/cat123 不工作
  5. home.php?id=blog&seq=456 变为 domain.com/status/456 不工作

正如您在上面看到的,只有数字 4 和 5 不起作用,它们只是显示未找到的索引或 404 页面,但是 Web 服务器上没有存储名称为 status 的目录em>

我怎样才能修复上面的代码以使它像上面的列表一样工作?

【问题讨论】:

    标签: php regex .htaccess mod-rewrite


    【解决方案1】:

    您需要更改/status/123 规则中的正则表达式,使其仅捕获数字和连字符并将/blog/this-is-a-blog-post 规则置于其下方。

    使用此代码:

    Options +FollowSymLinks -MultiViews
    RewriteEngine On
    
    # Displays directory if there is no / on the end of the URL
    RewriteCond %{REQUEST_URI} !^/(admin|status|customer) [NC]
    # Removes index.php from URL 
    RewriteCond %{THE_REQUEST} /index\.php [NC]
    RewriteRule ^(.*?)index\.php$ /$1 [L,R=302,NC,NE]
    
    # Rewrites /services to be /index.php?id=services
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^([a-zA-Z0-9-]+)/?$ /index.php?id=$1 [L,QSA]
    
    # Rewrites /blog/year2013/month12 to be /index.php?id=blog&year=2013&month=01
    RewriteRule ^([a-zA-Z0-9-]+)/year([0-9]+)/month([0-9]+)/?$ /index.php?id=$1&year=$2&month=$3 [L,QSA]
    
    # Rewrites /status/123 to be /index.php?id=status&seq=123
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^([a-zA-Z0-9-]+)/([\d-]+)/?$ /index.php?id=$1&seq=$2 [L,QSA]
    
    # Rewrites /blog/this-is-a-blog-post to be /index.php?id=blog&slug=this-is-a-blog-post
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^([a-zA-Z0-9-]+)/([\w-]+)/?$ /index.php?id=$1&slug=$2 [L,QSA]
    
    # Rewrites /status/cat1 to be /index.php?id=status&cat=1
     RewriteCond %{REQUEST_FILENAME} !-f
     RewriteCond %{REQUEST_FILENAME} !-d
     RewriteRule ^([a-zA-Z0-9-]+)/cat([0-9]+)?$ /index.php?id=$1&cat=$2 [L,QSA]
    

    【讨论】:

    • 当我转到 domain.com/status/cat1 时,我只显示 /status 页面,但如果我这样做 /status?cat=1 效果很好。当它显示 index.php?id=status 它包括 status.php 所以它显示的实际页面是 status.php?cat=1
    • 检查我的更新。不确定我的上述评论是否有意义
    • 好吧,可以证明我误解了你的一些退休。请求您提供在您的问题中不起作用的 URL,一旦我回到我的计算机并更新我的答案,我将对其进行测试。
    猜你喜欢
    • 2017-07-26
    • 1970-01-01
    • 2015-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-15
    • 1970-01-01
    相关资源
    最近更新 更多