【问题标题】:Redirect Rule in .htaccess php.htaccess php 中的重定向规则
【发布时间】:2021-06-24 07:47:58
【问题描述】:

我想将此 URL http://www.example.com/blog/Title-here 重定向到我的 blog.php 页面

请注意Title-here 可以是任何东西。

我该怎么做?

我正在尝试关注,但它不起作用。

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^blog/(.*)/?$ blog.php [NC,L]

我不知道为什么。

以下是我的完整 .htaccess 代码,可能是这里的问题。

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/(.*)/(.*)/?$ product.php [NC,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/(.*)/?$ results.php [NC,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ results.php [NC,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^blog/(.*)/?$ blog.php [NC,L]

ErrorDocument 404 http://www.example.com/404.php

【问题讨论】:

  • 您只想重定向到blog.php,不带标题?
  • @brombeer OP 可能意味着“重写”,而不是“重定向”。然后可以从请求的 URL 中读取标题(前端控制器模式)。
  • 你还有一个名为blog的目录吗?
  • @AmitVerma 不,只有 blog.php 文件

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


【解决方案1】:

对于您显示的示例,您能否尝试以下操作。由于您使用的是非常通用的正则表达式(其中没有给出任何 uri 条件),因此它没有达到您博客页面的最后一条规则。将其作为您的第一条规则,如下所示。我还在所有现有规则中修复了您的正则表达式。

请确保在测试您的 URL 之前清除您的浏览器缓存。

###Making Rewriteengine ON here.
RewriteEngine ON

##Placing rule for URIs starting from blog here.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^blog  blog.php [NC,L]

##Placing rule for url like: http://localhost:80/test1/test2/test3
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(?:[^/]*)/(?:[^/]*)/(?:.*)/?$ product.php [L]

##Placing rule for url like: http://localhost:80/test1/test2
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(?:[^/]*)/(?:.*)/?$ results.php [L]

##Placing rule for url like: http://localhost:80/test1
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ results.php [L]

ErrorDocument 404 http://www.example.com/404.php

【讨论】:

  • 您好,感谢您的快速回复,这个http://www.example.com/blog/title 仍然重定向到 404.php,所有其他(results.php,product.php)工作正常。
  • @AmanSharma,哦,好的,您的博客、php 文件在哪里?它是否位于 htaccess 文件旁边?请确认一次。
  • 两个文件都在根目录,同一个文件夹。
  • @AmanSharma,可以尝试将规则更改为RewriteRule ^blog/(.*)/?$ /blog.php [NC,L] 一次吗?请在测试您的 URL 之前清除您的浏览器缓存。告诉我进展如何。
  • 不,是一样的……重定向到 404.php 页面。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-11
  • 2017-08-29
  • 2012-12-24
  • 2010-12-22
  • 2011-01-05
相关资源
最近更新 更多