如何在子文件夹中重写“.htaccess”链接?

时间:2016-08-18 13:17:00

标签: php apache .htaccess

我有这个网站,结构如下:http://example.com/-/?id=1234。现在,我想将此链接编辑为:http://example.com/-/1234

所以,我在.htaccess中添加http://example.com/-/;有内容:

RewriteEngine On
RewriteRule ^/?-/([0-9]+)$ /index.php?id=$1

不幸的是,它始终显示:Error - 500

我的.htaccess有什么问题?

  

我的问题是与任何现有问题重复...

5 个答案:

答案 0 :(得分:0)

这是子文件夹的

<IfModule mod_rewrite.c>
    Options +FollowSymLinks
    RewriteEngine on
    RewriteBase /foldername
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?$1 [L]
</IfModule>

答案 1 :(得分:0)

您可以在/ - / .htaccess

中使用以下规则
RewriteEngine on
RewriteRule ^/?([0-9]+)$ /-/?id=$1 [L]

答案 2 :(得分:0)

这应该有效:

RewriteEngine On
RewriteRule ^-/([0-9]+)$ /-/?id=$1

它在我的测试服务器(Apache / 2.4.6)上正确重写,这里是重写跟踪日志(为了清楚起见,启用了重定向):

[rewrite:trace2]  init rewrite engine with requested uri /-/1234
[rewrite:trace1]  pass through /-/1234
[rewrite:trace3]  add path info postfix: /var/www/html/example.com/- -> /var/www/html/example.com/-/1234
[rewrite:trace3]  strip per-dir prefix: /var/www/html/example.com/-/1234 -> -/1234
[rewrite:trace3]  applying pattern '^-/([0-9]+)$' to uri '-/1234'
[rewrite:trace2]  rewrite '-/1234' -> '/-/?id=1234'
[rewrite:trace3]  split uri=/-/?id=1234 -> uri=/-/, args=id=1234
[rewrite:trace2]  explicitly forcing redirect with http://example.com/-/
[rewrite:trace1]  escaping http://example.com/-/ for redirect
[rewrite:trace1]  escaping id=1234 to query string for redirect id=1234
[rewrite:trace1]  redirect to http://example.com/-/?id=1234

答案 3 :(得分:0)

根据您编写的内容,您的目录结构是:

  "/" (root)
   |
   |___ "-" (directory)
         |
         |___ .htaccess
         |___ index.php

然而RewriteRule是这样的:

RewriteRule ^/?-/([0-9]+)$ /index.php?id=$1

使用目录结构,您已指定该规则永远不会匹配,除非您要求http://www.example.com/-/-/123之类的网址(注意2个连字符级别)。这是因为.htaccess使用的路径相对于文件 的路径,并且您已声明它位于名为&#34;的目录中。 - &#34; 。前导斜杠将跳转匹配到root的URL,并且因为您已经允许它0或1次(?)它将被忽略,因此,实际上,它&&#39; ll #39;在名为&#34; - &#34; 的目录中寻找名为&#34; - &#34; 的目录。

然后,如果您查看重写的实际位置,那么将转到/-/index.php,而转移到/index.php(域的根目录);领先的/ 在重写时生效。

规则几乎有意义如果您的索引和.htaccess文件位于docroot中:

  "/" (root)
   |
   |___ .htaccess
   |___ index.php

... &#34; - &#34; 目录实际上并不存在,但它只是用作URL中的重写触发器。

在这种情况下,你的RewriteRule应该是:

RewriteRule ^-/([0-9]+)$ /index.php?id=$1

这一切都不会导致HTTP 500错误 - 您还有一个问题,而您还没有向我们展示。

答案 4 :(得分:-1)

根据您的要求,这是您的服务器结构:

"/" (root)
|
|___ "-" (directory)
|
|___ .htaccess
|___ index.php

在制作.htaccess文件之前,请检查您的服务器配置。

RewriteEngine On
RewriteRule ^([0-9]+)$ ?x=$1