子文件夹中用于删除扩展名的.htaccess无效

时间:2017-05-19 13:30:05

标签: php .htaccess mod-rewrite

以下代码适用于根文件夹,这些文件夹特定于几页以删除扩展名。它的工作。

    RewriteEngine On

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-l

    RewriteRule ^About+$ about.php
    RewriteRule ^FAQ+$ faq.php
    RewriteRule ^Contact+$ contact.php
    RewriteRule ^Gallery+$ gallery.php
    RewriteRule ^Areas-we-service+$ cities.php


    RewriteRule ^(([^/]+/)*[^.]+)$ servicesparse.php?name=$1

然后我添加了一个php插件是名为product的子文件夹。     path是root-folder / php-product-catalog-module。插件中还有另一个htaccess文件。

#Fix Rewrite
Options -Multiviews

# Mod Rewrite
Options +FollowSymLinks
RewriteEngine On
RewriteBase /php-product-catalog-module/

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

# used for viewing single product page
RewriteRule ^product/([a-zA-Z0-9_-]+)/([0-9]+)\/?$ product.php?name=$1&id=$2

# used for php pages such as "yoursite.com/login.php" will become "yoursite.com/login/"
RewriteRule ^([a-z_]+)\/?$ $1.php [NC]

因此子文件夹的htaccess无效。 它不会将http://cp2.designnrank.com/~wolverin/php-product-catalog-module/login.php重定向到http://cp2.designnrank.com/~wolverin/php-product-catalog-module/login

其他产品页面的规则也不起作用。

1 个答案:

答案 0 :(得分:0)

您确定启用了htaccess吗?由于性能问题,最近在Apache中默认禁用了htaccess文件。启用.htaccess文件后,Web服务器必须在其所在的每个子目录中的每次命中时检查它。

启用htaccess可以通过在<Directory></Directory>指令中将“AllowOverride None”替换为“AllowOverride All”来完成。

<Directory /var/www/>
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
</Directory>

更好的方法是将所有重写规则放入虚拟主机配置文件中。类似的东西:

<VirtualHost *:80>

    DocumentRoot /var/www/mywebsite

    ErrorLog ${APACHE_LOG_DIR}/mywebsite-error.log
    CustomLog ${APACHE_LOG_DIR}/mywebsite-access.log combined

    #Fix Rewrite
    Options -Multiviews

    # Mod Rewrite
    Options +FollowSymLinks
    RewriteEngine On
    RewriteBase /php-product-catalog-module/

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

    # used for viewing single product page
    RewriteRule ^product/([a-zA-Z0-9_-]+)/([0-9]+)\/?$ product.php?name=$1&id=$2

    # used for php pages such as "yoursite.com/login.php" will become "yoursite.com/login/"
    RewriteRule ^([a-z_]+)\/?$ $1.php [NC]

</VirtualHost>