【问题标题】:Rewrite rules html/php重写规则 html/php
【发布时间】:2015-03-04 21:30:30
【问题描述】:

我目前有这个 .htaccess 代码来删除 .php/.html 扩展名。

# Apache Rewrite Rules
 <IfModule mod_rewrite.c>
  Options +FollowSymLinks
  RewriteEngine On
  RewriteBase /

# Add trailing slash to url
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/|#(.*))$
  RewriteRule ^(.*)$ $1/ [R=301,L]

# Remove .php-extension from url
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME}\.php -f
  RewriteRule ^([^\.]+)/$ $1.php 

  # Remove .html-extension from url
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME}\.html -f
  RewriteRule ^([^\.]+)/$ $1.html 


# End of Apache Rewrite Rules
 </IfModule>

我正在尝试将 poll.php 页面设置为 /poll/123123/

我尝试添加

RewriteRule ^[A-Za-z0-9-]+/([A-Za-z0-9-]+)/?$ polls.php?poll=$1 [NC,L]

但它随后会尝试检测dashboard.php 而不是dashboard.html。

任何帮助将不胜感激,谢谢。

【问题讨论】:

    标签: php html .htaccess rewrite


    【解决方案1】:

    如果您正在寻找一个特定的 URL 并且只有数字发生变化,则不需要通配符

     RewriteRule ^poll/(\d+)/$ polls.php?poll=$1
    

    或者更好的是总是得到 6 位数字

    RewriteRule ^poll/([0-9]{6})/$ polls.php?poll=$1
    

    应该这样

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-10
      • 2011-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-22
      相关资源
      最近更新 更多