1

我有两个不同的 .htaccess 文件,每个文件都做不同的事情,当我将它们组合在一起时,它们将无法一起工作。

第一个允许我输入 www.mydoman.com/test 并表示 www.mydomain.com/index.php?page=test

RewriteRule    ^([A-Za-z0-9-_]+)/?$  index.php?subclass=$1 [NC,L]
RewriteRule    ^.* index.php [NC,L]

第二个文件切断了 .html 和 .php 文件的扩展名,因此可以使用 www.mydomain.com/about 插入 www.mydoman.com/about.php

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On

RewriteBase /cl_clone

## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]

## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]

# e.g. example.com/foo will display the contents of example.com/foo.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+)$ $1.html [L,QSA]

有没有办法让这两者在一个 .htaccess 文件中一起工作?

4

1 回答 1

1

您在顶部有一个重定向规则,然后有 2 组规则将 php 或 html 扩展名添加到请求的末尾(如果存在这样的文件)。然后您的其他规则为 index.php 进行路由。

有些事情你需要解决。在这两种情况下,您的重写基础/cl_clone似乎根本不起作用,因为您的 URLcl_clone在请求中没有(例如,它们只是www.mydomain.com/about,没有 cl_clone)。

所以你可能想删除它。

否则,您可以简单地将index.php路由规则添加到您拥有的其他规则的最底部。您可能需要添加额外的调整,也许:

RewriteRule    ^([A-Za-z0-9-_]+)/?$  index.php?subclass=$1 [NC,L]
RewriteCond %{REQUEST_URI} !index.php
RewriteRule    ^.* index.php [NC,L]

或者改变你RewriteBase /cl_cloneRewriteBase /

于 2013-01-04T23:02:18.240 回答