簡體   English   中英

將子目錄網址重寫為文件

[英]Rewrite subdirectory url to file

我有一個名為test的子目錄, http://www.mywebsite.com/test/ 目錄的內部是一個index.php文件。

我想在.htaccess文件中放入將重寫URL的測試子目錄,以便http://www.mywesbite.com/test/paul變為http://www.mywebsite.com/test?author=paul ,因此諸如http://www.mywebsite.ocom/test/sam/3之類的網址將變為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 Not Found錯誤。 我想念什么嗎? 任何想法都非常感謝!

編輯 :我檢查了phpinfo()和mod_rewrite似乎確實顯示為已加載的模塊。

這也是我的虛擬主機文件的外觀。 看起來還設置了AllowOverride。

<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文件。 。

我測試了巴拿馬傑克的解決方案,似乎RewriteRules並未考慮到已經存在於/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]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM