简体   繁体   中英

Rewrite subdirectory url to file

I have a subdirectory called test, http://www.mywebsite.com/test/ . Inside of the directory is an index.php file.

I want to put an .htaccess file inside of the test subdirectory that will rewrite the URLs so that http://www.mywesbite.com/test/paul will become http://www.mywebsite.com/test?author=paul , and so that urls like http://www.mywebsite.ocom/test/sam/3 will become http://www.mywebsite.com/test?author=sam&test=3

This is what I have so far:

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]

Unfortunately, I get a 404 Not Found error. Am I missing something? Any thoughts are much appreciated!

EDIT : I checked phpinfo() and mod_rewrite does seem to be showing as a loaded module.

This is what my virtual host file looks like too. It looks like AllowOverride is set as well.

<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>

EDIT 2 : I also wanted to mention, I put some junk text in the .htaccess file to try and break it, and instead of a 404 error I got an internal server error, so it does seem like the .htaccess file is being read.

I tested Panama Jack's solution and it seems like the RewriteRules are not accounting for this already being in /test/.htaccess... so this would not work for the first example (/test/paul). Proposing an alternate solution that should work for both examples:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM