用子目录重写url
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用子目录重写url相关的知识,希望对你有一定的参考价值。
我在web目录中有这个域的域主项目。现在我需要在域根的子目录中放置类似的项目。
domain.com
|-web
| |-index.php
| |-assets
|
|-subdirectory
| |-web
| |index.php
| |-assets
我的第一个项目domain.com/homepage的示例网址是使用htaccess重写到domain.com/web/index.php?url=homepage。资产网址将重写为资产目录。
/.htaccess
RewriteEngine On
RewriteRule ^(.*)$ web/$1 [L]
/Web/.htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} .(gif|png|jpe?g|ico|swf|js|css)$ [NC]
RewriteRule .* - [R=404,L]
# rewrite assets version to orginal location
RewriteCond %{REQUEST_URI} assets/([0-9]+)/
RewriteRule ^assets/([0-9]+)/(.*)$ assets/$2
RewriteCond %{REQUEST_URI} !.(gif|jpg|png)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?url=$1 [L,QSA]
它运行良好,但如何扩展此htaccess或在子目录中添加另一个.htaccess来重写子目录网址?
示例domain.com/subdirectory/homepage sholud要重写到domain.com/subdirectory/web/index.php?url=homepage
解:
/.htaccess和/subdirectory/.htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ web/$1 [L,QSA]
</IfModule>
/web/.htaccess和/subdirectory/web/.htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} .(gif|png|jpe?g|ico|swf|js|css)$ [NC]
RewriteRule .* - [R=404,L]
# rewrite assets version to orginal location
RewriteCond %{REQUEST_URI} assets/([0-9]+)/
RewriteRule ^assets/([0-9]+)/(.*)$ assets/$2
RewriteCond %{REQUEST_URI} !.(gif|jpg|png)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1 [L,QSA]
</IfModule>
最后:
domain.com/start重写为domain.com/web/index?url=start
domain.com/subdirectory/start重写为domain.com/subdirectory/web/index?url=start
答案
排除根目录上的/subdirectory
重写,并将htaccess
规则应用于与根目录相同的subdirectory
文件夹下。
/.htaccess
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/subdirectory
RewriteRule ^(.*)$ web/$1 [L]
以上是关于用子目录重写url的主要内容,如果未能解决你的问题,请参考以下文章