【问题标题】:Failing to prettify URL with htaccess无法使用 htaccess 美化 URL
【发布时间】:2021-10-01 17:31:17
【问题描述】:

我正在尝试美化我的网址

来自:http://localhost/blog.php?id=1

收件人:http://localhost/blog/1

我已将以下内容添加到我的 .htaccess 中

Options +FollowSymLinks
RewriteEngine On

RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f

RewriteRule ^blog/([a-zA-Z0-9]+)$ blog.php?id=$1 [NC,L]

据我了解,当我在 PHP header("location: /blog/".id); 中调用我的“to”url 时,现在应该重定向它会将我带到相关页面,并且我的$_GET['id'] 应该有一个值。空出来的

我一直在搞乱一些不同的正则表达式,但我认为这不是问题。

在 apache 配置中,我将“AllowOverride”更改为“All”

为了检查 .htaccess 是否被使用,我输入了一些无效的正则表达式,并在 apache_error.log 中出现错误

我在很多不同的页面上都问过同样的问题,并且观看了很多 YouTube 视频,但我看不出我错过了什么

【问题讨论】:

  • 您能否确认一下您是否在浏览器中点击链接localhost/blog.php?id=1?你想在浏览器中将其更改为链接localhost/blog/1 链接吗?
  • @RavinderSingh13 "blog.php?id=1" 工作成功。是的,我想把它改成“blog/1”

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


【解决方案1】:

以下列方式保存您的 htaccess 规则文件。请确保在测试您的 URL 之前清除您的浏览器缓存。

Options +FollowSymLinks -MultiViews
RewriteEngine On
##Rules for external redirect to blog/1 here.
RewriteBase /    
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{THE_REQUEST} \s/(blog)\.php\?id=(\d+)\s [NC]
RewriteRule ^ /%1/%2? [R=301,L]

##Rules for internal rewrite to blog.php file here.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^blog/([\w-]+)/?$ blog.php?id=$1 [QSA,NC,L]

##Rules for internal rewrite to index.php file here.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)/?$ index.php?id=$1 [QSA,NC,L]

【讨论】:

  • 从某种意义上说,它现在可以将我的 URL 转换为我所要求的内容,但实际上并没有加载任何页面。在所有 URL 上获取“未找到”
  • @Oyyou,好的,你能告诉我你点击的是什么示例网址吗?以blog 开头的网址也会由 blog.php 提供,对吗?请在这些方面向我确认,干杯。
  • @Oyyou,另外请确保您的 htaccess 文件存在于您的 Apache 服务器中的 blogs.php 文件之外。
  • 我的 .htaccess 文件与 index.php 和 blog.php 一起位于基本目录中。现在,当我输入“../blog?id=1”时,它会将我带到“..blog/1”。这是我要求的。但是现在没有找到任何页面。当我删除“-MultiViews”时,一切都找到了,我的 URL 也发生了变化,但 $_GET["id"] 没有值
  • @Oyyou,好的,我现在已经改变了规则,请您检查一下并告诉我情况如何?
【解决方案2】:

试试这个。 1.

Options +FollowSymLinks
RewriteEngine on
RewriteRule blog/id/(.*)/ blog.php?id=$1
RewriteRule blog/id/(.*) blog.php?id=$1 

示例网址:- http://localhost/blog/id/1/

目录类型网址

【讨论】:

  • 恐怕运气不好!
猜你喜欢
  • 2017-04-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-16
  • 1970-01-01
  • 2016-01-11
  • 2013-07-25
相关资源
最近更新 更多