【问题标题】:URL Change .htaccess Code Not WorkingURL 更改 .htaccess 代码不起作用
【发布时间】:2014-02-16 00:01:19
【问题描述】:

我想改变:

localhost/article.php?id=bryan

简单来说:

localhost/article/bryan

如果是这样的:

localhost/article.php?id=Mark%20Henry%20is%20Drawing

我需要将其更改为:

localhost/article/mark-henry-drawing

这里的一个人给了我这个代码来使用:

RewriteEngine On
RewriteRule ^article/([^/]*)$ /article.php?id=$1 [L]

每个人都说这应该可以解决问题,但每次输入时我仍然会收到内部服务器错误:

localhost/article/bryan

如果有帮助的话,我正在使用 WAMP 服务器,以下代码是我的整个 .htaccess 文件:

Options +FollowSymLinks
RewriteEngine On

# Removes the .php extension from pages
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) $1.php [L]

# Cleans up the PHP URL of all pages on website
RewriteRule ^article/([^/]*)$ /article.php?id=$1 [L]

我不知道还能尝试什么。

【问题讨论】:

  • “但我仍然遇到内部服务器错误” – 然后锁定服务器的错误日志!

标签: regex .htaccess url wampserver


【解决方案1】:

您当前的.htaccess 的问题在于它进入了导致内部服务器错误的循环。

要使您希望的工作正常,您需要在请求中捕获 URL 并将其重定向,然后在内部将其重定向回来:

Options +FollowSymLinks -MultiViews

RewriteEngine On
RewriteBase /

# Redirect /article.php?id=bryan to /article/bryan
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+article\.php\?id=([^\s]+) [NC]
RewriteRule ^ /article/%1? [R=302,L]

# Internally forward /article/bryan to /article.php?id=bryan
RewriteRule ^article/([^/]+)/?$ /article.php?id=$1 [L,NC]

但是,没有简单的方法将 %20 转换为空格或 -,您必须从系统中解决。

【讨论】:

  • 那么在我的系统上运行它是什么意思?没有通用的方法吗?
  • @user3310212 not with .htaccess 它们会过于复杂和昂贵以至于不值得使用,因为它会给服务器带来很大的负担,您必须在代码端进行转换使其在链接或其他方式上破折号。
猜你喜欢
  • 2015-09-06
  • 1970-01-01
  • 1970-01-01
  • 2017-11-09
  • 1970-01-01
  • 2016-04-11
  • 2011-03-16
  • 2019-02-09
  • 1970-01-01
相关资源
最近更新 更多