apache - .htaccess php 中的重定向规则

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

我想将此 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

最佳答案

根据您展示的示例,您能否尝试以下操作。由于您使用的是非常通用的正则表达式(未在其中提供任何 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

关于apache - .htaccess php 中的重定向规则,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66834602/

相关文章:

php - 当PHP运行模块时,Apache无法启动

php - htaccess 在 php 和 angularjs 中配置路由

regex - 允许特定文件通过 https 运行

tomcat 前面的 apache 和重定向无法正常工作

apache - 如何防止 Netbeans 因端口已被使用而无法启动 Tomcat?

apache - DBus 是我要找的吗?

.htaccess - 在 .htaccess 中一起使用 mod_rewrite 和 mod_alias(重定向 301)?

apache - 如何使用 htaccess 重定向图像更改路径?

php - 重定向所有以/5 开头的网址

ruby-on-rails - 在 Rails for Heroku 中将 URL 从子目录重写为子域