dtoqa66028 2015-01-28 00:18
浏览 6
已采纳

mod_rewrite对没有正斜杠的链接的影响?

I have the below VirtualHost and HTML. The purpose of the VirtualHost is to take http://example.com/bla/index.php?id=3 and rewrite it as http://example.com/index.php?admin=bla&id=3. If the URL in the browser is http://example.com/bla/, both the form and the links go to http://example.com/bla/index.php. But if I put http://example.com/bla (without the trailing forward slash) in the browser, the posts go to http://example.com/bla/index.php, but the the links go to http://example.com/index.php (without the "bla"). Why is this, and what do I do to direct the links to http://example.com/bla/index.php?

<VirtualHost *:80>
    ServerName example.com
    DocumentRoot /var/www/html
    <Directory "/var/www/html">
        allow from all
        Options +Indexes
        RewriteEngine On
        RewriteBase /

        # If the request is for a valid directory, file, or link, don't do anything
        RewriteCond %{REQUEST_FILENAME} -d [OR]
        RewriteCond %{REQUEST_FILENAME} -f [OR]
        RewriteCond %{REQUEST_FILENAME} -l
        RewriteRule ^ - [L]

        RewriteRule ^([^/]*|[^/]+/(index.php)?)$ /index.php?admin=$1 [L,QSA]
    </Directory>
</VirtualHost>

index.php

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>Links</title>
    </head>
    <body>
        <a href="index.php?id=1">link 1</a>
        <a href="index.php?id=2">link 2</a>
        <a href="index.php?id=3">link 3</a>
        <form method="post">
            <input type="text" name="post_text">
            <input type="submit" value="submit">
        </form>
    </body>
</html>
  • 写回答

1条回答 默认 最新

  • douwen8118 2015-01-28 01:09
    关注

    It's not a problem or a bug.

    With the link the browser does the job

    With http://example.com/bla : The link index.php
    in the root folder -> http://example.com/index.php

    With http://example.com/bla/ : The link index.php
    in the bla folder -> http://example.com/bla/index.php

    With the form the server does the job

    The browser send form to http://example.com/bla or http://example.com/bla/ and the server rewrite.

    You can use this in your html:

        <a href="/bla/index.php?id=3">link 3</a>
        <form method="post" action="/bla/index.php">
    

    or in the head:

      <base href="/bla/">
    

    to avoid problems

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 自适应 LMS 算法实现 FIR 最佳维纳滤波器matlab方案
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像