使用带变量的htaccess重定向

时间:2015-09-18 15:03:53

标签: .htaccess redirect

我想将我的所有php文件重定向到没有php扩展名。 喜欢news.php的新闻或articles.php文章。我知道这个htacces命令:

WITH cte AS (
  SELECT Title, MAX([Date]) d, ROW_NUMBER() OVER(
    PARTITION BY Title ORDER BY DATEFROMPARTS(YEAR([Date]), MONTH([Date]), 1) DESC) rn
  FROM t1
  GROUP BY Title, DATEFROMPARTS(YEAR([Date]), MONTH([Date]), 1)
)
SELECT t.Title, MAX(CASE WHEN cte.rn = 1 THEN t.Value END) m1,
  MAX(CASE WHEN cte.rn = 2 THEN t.Value END) m2,
  MAX(CASE WHEN cte.rn = 3 THEN t.Value END) m3
FROM cte INNER JOIN t1 t ON
  t.Title = cte.Title AND t.[Date] = cte.d
GROUP BY t.Title

但是我想用变量做这样的事情但不起作用:

Redirect 301 /news.php /news

2 个答案:

答案 0 :(得分:1)

您可以使用重写模块:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule 301 ^(.*)\.php$ $1 [L,QSA]

答案 1 :(得分:0)

您需要一个重定向规则来删除.php扩展名和重写规则,以便静默添加.php扩展名。在根目录中尝试这两个规则.htaccess:

RewriteEngine On

# To externally redirect /dir/file.php to /dir/file
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R,L]

# To internally redirect /dir/file to /dir/file.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]