将子目录 url 重写为文件


Rewrite subdirectory url to file

我有一个名为test,http://www.mywebsite.com/test/的子目录。 目录内部是一个索引.php文件。

我想在测试子目录中放置一个 .htaccess 文件,该文件将重写 URL,以便 http://www.mywesbite.com/test/paul 变得 http://www.mywebsite.com/test?author=paul,并且像 http://www.mywebsite.ocom/test/sam/3 这样的 url 将变得 http://www.mywebsite.com/test?author=sam&test=3

这是我到目前为止所拥有的:

RewriteEngine On
Options +FollowSymlinks
RewriteCond %{REQUEST_URI} (/test/)
RewriteRule ^(.*)/(.*)/(.*)/?$ /test/index.php?author=$1&page=$2 [L]
RewriteCond %{REQUEST_URI} (/test/)
RewriteRule ^(.*)/(.*)/?$ /test/index.php?author=$1 [L]

不幸的是,我收到404未找到错误。 我错过了什么吗? 任何想法都非常感谢!

编辑:我检查了phpinfo(),mod_rewrite似乎显示为加载的模块。

这也是我的虚拟主机文件的样子。 看起来也设置了允许覆盖。

<VirtualHost *:80>
    ServerName mywebsite.com
    ServerAlias www.mywebsite.com
    DocumentRoot /var/www/html/mywebsite.com/httpdocs
    ErrorLog /var/log/apache2/mywebsite.com-error_log
    CustomLog /var/log/apache2/mywebsite.com-access_log combined
    HostnameLookups Off
    UseCanonicalName Off
    ServerSignature On
    ScriptAlias /cgi-bin/ "/var/www/html/mywebsite.com/httpdocs/"
    <Directory "/var/www/html/mywebsite.com/httpdocs/">
        Options Indexes FollowSymLinks
        AllowOverride All
        Order allow,deny
        Allow from all
    </Directory>
    <IfModule mod_userdir.c>
        UserDir public_html
        Include /etc/apache2/mod_userdir.conf
    </IfModule>
</VirtualHost>

编辑2:我还想提一下,我在.htaccess文件中放了一些垃圾文本来尝试破解它,而不是404错误,我得到了内部服务器错误,所以看起来确实正在读取.htaccess文件。

我测试了巴拿马杰克的解决方案,似乎重写规则没有考虑到这一点已经在/test/.htaccess 中...所以这不适用于第一个示例(/test/paul)。提出一个应该适用于这两个示例的替代解决方案:

RewriteEngine On
RewriteBase /test/
RewriteRule ^(.+)/(.+)/?$ index.php?author=$1&page=$2 [L]
RewriteCond %{REQUEST_URI} !index'.php$
RewriteRule ^(.+)/?$ index.php?author=$1 [L]