使一些php文件指向子目录(site/category/pagename.php)


htaccess to make SOME php files point to a subdirectory (site/category/pagename.php)

有很多htaccess重写的问题,但我找不到一个直接的解决方案来解决我的问题(我不知道如何编码的htaccess做我想要的)。

所以,我做的第一件事,是摆脱所有php扩展使用…

Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
# If folder does not exist
RewriteCond %{REQUEST_FILENAME} !-d
# and file exist
RewriteCond %{DOCUMENT_ROOT}/$1'.php -f
# uncomment the below rule if you want / not optional 
# otherwise leave as is
# RewriteRule ^([^/]+)/$ $1.php [L]
# internally show the content of filename.php
RewriteRule ^([^/]+)/?$ $1.php [L]

这似乎有效。所以,如果我去www.myplace.com/pagename.php…url将被重写为www.myplace.com/pagename/这对我网站上的大多数页面来说都很好。

但是现在,我的根目录中有几个页面,我想"出现"在子目录中。因此,像www.myplace.com/pagesub.php这样的页面会将URL重写为www.myplace.com/category/pagesub/

只有5个页面,我需要这样做,所以代码不需要是一个全局的变化。我仍然需要所有其他页面保持为常规文件夹(如maincat.php更改为www.myplace.com/maincat/)

如果我执行以下操作,它只会将php页面指向一个空目录,并出现页面丢失错误:

RewriteRule    ^page1sub?$    /category/page1sub/    [NC,L]
RewriteRule    ^page2sub?$    /category/page2sub/    [NC,L]
RewriteRule    ^page3sub?$    /category/page3sub/    [NC,L]
RewriteRule    ^page4sub?$    /category/page4sub/    [NC,L]
RewriteRule    ^page5sub?$    /category/page5sub/    [NC,L]

谁能告诉我怎么让它工作?

你试过这样做吗?

,

RewriteCond %{page1sub} !^/subfolder/

Redirect /olddirectory http://yoursite.com/newdirectory/

编辑

我不确定,但试试这样做:

RewriteCond %{page1sub} ^GET' /category/page1sub/ 
RewriteRule ^page1sub/(.*) /category/page1sub/    [NC,L]

/page1sub/…的请求将从外部重定向到/category/page1sub/

我假设你有一个index.php文件在每个子目录?如果您尝试在末尾添加index.php,或者您试图指向的任何文件

RewriteRule    ^page1sub?$    /category/page1sub/index.php    [NC,L]

编辑

RewriteRule ^category/page1sub page1sub.php [NC,L]

这个规则应该对你有用。

URL读取为"www.mysite.com/category/page1sub",并从"page1sub.php"获取内容。

我在这里测试了一下,但我不会让它永远保持不变,但你可以看到它是有效的