mod_rewrite 对没有正斜杠的链接的影响?
Affect of mod_rewrite on links without a forward slash?
我有下面的 VirtualHost 和 HTML。 VirtualHost 的目的是 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= [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>
这不是问题或错误。
使用 link 浏览器完成工作
与 http://example.com/bla
:
linkindex.php
在根文件夹中 -> http://example.com/index.php
与 http://example.com/bla/
:
linkindex.php
在 bla
文件夹中 -> http://example.com/bla/index.php
使用表单服务器完成工作
浏览器将表单发送到 http://example.com/bla
或 http://example.com/bla/
,然后服务器重写。
您可以在 html:
中使用它
<a href="/bla/index.php?id=3">link 3</a>
<form method="post" action="/bla/index.php">
或头部:
<base href="/bla/">
避免问题
我有下面的 VirtualHost 和 HTML。 VirtualHost 的目的是 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= [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>
这不是问题或错误。
使用 link 浏览器完成工作
与 http://example.com/bla
:
linkindex.php
在根文件夹中 -> http://example.com/index.php
与 http://example.com/bla/
:
linkindex.php
在 bla
文件夹中 -> http://example.com/bla/index.php
使用表单服务器完成工作
浏览器将表单发送到 http://example.com/bla
或 http://example.com/bla/
,然后服务器重写。
您可以在 html:
中使用它 <a href="/bla/index.php?id=3">link 3</a>
<form method="post" action="/bla/index.php">
或头部:
<base href="/bla/">
避免问题