.htaccess用子目录重写不起作用


.htaccess rewrite with sub directories not working

我想从重写我的url

http://mysite.com/index.php?node=home&photoID=10

至:

http://mysite.com/home/10

目前,仅针对

http://mysite.com/home

我使用

RewriteRule ^([^/]*)$ index.php/?node=$1 [L]

但是当我尝试使用时

RewriteRule ^([^/]*)/([^/]*)$ index.php/?node=$1&id=$2 [L]

它似乎没有正确加载我的页面,页面没有样式或其他什么。

(另外,我的.htaccess文件的顶部有:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

如果我只是去mysite.com/home,它显示404未找到。如果不总是有一个子页面,我该怎么做?

首先要有这样的.htaccess规则:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
## If the request is for a valid directory
RewriteCond %{REQUEST_FILENAME} -d [OR]
## If the request is for a valid file
RewriteCond %{REQUEST_FILENAME} -f [OR]
## If the request is for a valid link
RewriteCond %{REQUEST_FILENAME} -l
## don't do anything
RewriteRule ^ - [L]
RewriteRule ^([^/]+)/([^/]+)/?$ index.php/?node=$1&id=$2 [L,QSA]
RewriteRule ^([^/]+)/?$ index.php/?node=$1 [L,QSA]

然后确保在<head></head>:之间的页面顶部添加此行

<base href="http://mysite.com/">

您需要添加一个<base>标记,让您的相对URL知道URI基在哪里。由于您已将URI路径从/home更改为/home/10,因此相对链接的基变为/home。您需要将此添加到页面的标题中:

<base href="/">