【问题标题】:Is it possible to allow only the correct pretty url?是否可以只允许正确的漂亮网址?
【发布时间】:2015-09-03 06:30:13
【问题描述】:

我有这些漂亮的网址:

  • 应用/类别/概览
  • 应用/类别/新
  • app/category/edit/4

  • 应用/产品/概览

  • 应用/产品/新
  • app/product/edit/4

.htaccess

RewriteEngine On
RewriteRule ^([a-z]+)/([a-z]+)/?([0-9]*)$ index.phpcontroller=$1&action=$2&id=$3 [NC,L]

是否可以定义任何其他 url 回到索引?

p.e. app/i/am/the/fastest/man/alive ---> app/

我们将不胜感激!

【问题讨论】:

  • 总是以/app开头吗?
  • 在此代码之后,redirect everything to the index.
  • 这是我的应用程序的名称。本地主机/应用程序
  • 不要破坏网络。 404 响应是有原因的。

标签: php apache .htaccess mod-rewrite friendly-url


【解决方案1】:

您可以为此制定新规则:

RewriteEngine On
RewriteBase /app/

RewriteRule ^([a-z]+)/([a-z]+)/?([0-9]*)$ index.php?controller=$1&action=$2&id=$3 [NC,L,QSA]

# If the request is not for a valid directory
RewriteCond %{REQUEST_FILENAME} !-d
# If the request is not for a valid file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

我添加了?在 index.php 之后

【讨论】:

    【解决方案2】:

    你的问题的答案

    是否可以只允许正确的漂亮 url?

    排序,但您必须更具体地规定您的规则才能将它们限制在此范围内。举这个例子。

    RewriteEngine On
    RewriteBase /app
    
    #rewrite rule if starts with /app/product or /app/category
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(product|category)/([a-z]+)/([0-9]*)/?$ index.php?controller=$1&action=$2&id=$3 [NC,L]
    #else redirect to homepage
    RewriteRule . app/ [R=301,L]
    

    这将确保只有类似 http://example.com/app/product/1 的 URL 将被重写,而所有其他类似 http://example.com/app/some/other/page 的 URL 将被重写到您的主页。

    【讨论】:

      猜你喜欢
      • 2014-04-22
      • 2012-03-07
      • 1970-01-01
      • 2017-07-27
      • 2011-02-04
      • 2011-10-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多