【问题标题】:Htacces url rewriting not workingHtaccess url重写不起作用
【发布时间】:2015-02-03 20:52:50
【问题描述】:

我一直试图弄清楚 urlrewriting。而且我还没有真正明白我的想法。 所以我尝试了这个:

Options -Indexes +ExecCGI
AddHandler cgi-script .cgi .pl
php_flag display_startup_errors on
php_flag display_errors on
php_flag html_errors on
php_flag  log_errors on
Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteRule ^home/? index.php
RewriteRule ^forum/?$ forum.php [L,NC]
RewriteRule ^forum/([a-z0-9-]+)/?$ forum.php?catagory=$1 [L,QSA,NC]
RewriteRule ^login/? loginpage.php
RewriteRule ^register/? registerpage.php
RewriteRule ^servers/? servers.php
RewriteRule ^profile/? profile.php
RewriteRule ^profile/([A-Za-z0-9-]+)/?$   profile?user=$1
RewriteRule ^members/? memebers.php

我显然没有工作,我的页面甚至无法正常加载

当我使用普通网址时:

forum/?catagory=test

这非常好用,可以向我展示一切。我不明白为什么 url 重写不起作用!甚至尝试了发电机和其他一些东西。它不会工作

【问题讨论】:

  • 你有RewriteEngine OnRewriteBase /
  • 第一件事,是的。我现在添加的第二件事,但没有帮助。
  • 如果你去/forum/test会发生什么?您收到 404 错误或 forum.php 加载时没有类别?
  • @Nytrix:/forum/ 文件夹中也有 .htaccess 吗?
  • @anubhava 没有这样的文件夹。这是forum.php的重写

标签: html .htaccess url url-rewriting


【解决方案1】:

首先,您是否打开了 RewriteEngine? 其次,是否启用了重写模块?做一个phpinfo();来检查它和这个answer

这边:

RewriteEngine On
RewriteBase /
RewriteRule ^forum/?$ forum.php
RewriteRule ^forum/([A-Za-z0-9-]+)/?$ forum.php?catagory=$1 [L]

这个页面可以帮助你:URL Rewriting for Beginners

【讨论】:

  • 我打开了引擎并启用了它:据说“/forum/?catagory=test”有效。我试过你的代码,还是不行。
  • 您的日志中是否有错误输出可以提供帮助?
  • 是的,我加载的所有内容都得到了一个无法加载的错误:“加载资源失败:服务器响应状态为 404(未找到)”
【解决方案2】:

关闭MultiViews 选项:

Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteRule ^forum/?$ forum.php [L,NC]
RewriteRule ^forum/([a-z0-9-]+)/?$ forum.php?catagory=$1 [L,QSA,NC]

选项MultiViewsApache's content negotiation module 使用,它在mod_rewrite 之前运行,并使Apache 服务器匹配文件的扩展名。所以/file 可以在 URL 中,但它会服务于/file.php

【讨论】:

  • 我在问题中添加了整个 .htacces。页面 test.com/forum/test 不起作用。我明白了:prntscr.com/5drsb8。我应该得到这样的东西:prntscr.com/5drsg3
  • ok 看起来问题是没有得到你的 css/js。这是由于您使用了 css/js/images 的相对链接。您可以尝试在页面的 HTML 标头中添加:<base href="/" />,以便从该 URL 而非当前页面的 URL 解析每个相对 URL。
  • 我会试试的!唯一奇怪的是,如果我正常输入:“test.com/forum/?catagory=test”,效果很好。
  • 它不起作用,你是对的,它从错误的东西中获取 css/js/图片:prntscr.com/5dru84。但你的回答没有奏效..
猜你喜欢
  • 2014-08-08
  • 2011-07-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多