大网数据广告活动
网站运营与故障问答

subdirectory htaccess url rewrites

· 52CE 我爱测

I have a project with this skimmed structure: -proj -----pages ---------example -------------pages -------------.htaccess ---------.htaccess I managed to get it working so that all url's go to /pages from the get-go using my proj…

问题原文

原帖链接 · treyBake · CC BY-SA 4.0

I have a project with this skimmed structure:

-proj
-----pages
---------example
-------------pages
-------------.htaccess
---------.htaccess

I managed to get it working so that all url's go to /pages from the get-go using my project root .htaccess:

RewriteEngine On
RewriteBase /

RewriteCond %{ENV:REDIRECT_STATUS} . [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]

# rewrite /pages/area/index.php url -> /area/
RewriteCond %{REQUEST_URI} /(.+)
RewriteRule !\.[a-z0-4]{2,4}$ /pages/%1index.php [NC,L]

# remaining req's get rewritten to /pages/ -> now we don't need index.php in tree root
RewriteRule (.*) /pages/$1 [L]

This code was taken from an answer elsewhere and as I'm no htaccess guru, I thought I could use it in my subdirectory .htaccess file:

RewriteEngine Off
RewriteEngine On

RewriteCond %{ENV:REDIRECT_STATUS} . [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]

# rewrite /pages/area/index.php url -> /area/
RewriteCond %{REQUEST_URI} /(.+)
RewriteRule example/pages/\.[a-z0-4]{2,4}/$ /example/pages/%1index.php [NC,L]

# remaining req's get rewritten to /pages/ -> now we don't need index.php in tree root
RewriteRule ^example/(.*) /example/pages/$1 [L]

But going to /example/pages/ in my browser shows the index.php instead of 404'ing as it should be trying to look for a folder called pages within the pages directory - but it seems no.

I could try and add this all to the project root .htaccess and it would be a lot simpler to achieve - however, I'm aiming for my project design to be modular, so these example area's can be added/removed whenever.

How do I rewrite http://localhost/area/pages/ into http://localhost/area/ using the .htaccess file in the subdirectories under the top-level pages directory?

已采纳解答

原帖链接 · MrWhite · CC BY-SA 4.0

You need the .htaccess file in the document root in order to rewrite a request of the form /area/somethingelse.php to the /pages subdirectory in the root (ie. /pages/area/somethingelse.php) before the .htaccess file in the subdirectory (ie. /pages/area/.htaccess) is able to rewrite the request a second time to /pages/area/pages/somethingelse.php - the eventual target.

I have a project that currently routes everything to the /pages directory.

However, if this is an entirely separate "project" then you have to be careful of conflicts. Your directives are also rewriting to an index.php file - but you make no reference to this in your question? For the sake of simplicity, I'm going to ignore that for now(!) and simply rewrite to the /pages (and /pages/example/pages) subdirectories.

Try something like the following instead:

/.htaccess (file in the document root)

RewriteEngine On

# Do not process rewritten requests OR requests that map to existing files
RewriteCond %{ENV:REDIRECT_STATUS} . [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]

# Rewrite EVERYTHING to the /pages subdirectory
RewriteRule ^ /pages%{REQUEST_URI} [L]

/pages/example/.htaccess (file in the subdirectory)

RewriteEngine On

# Do not process requests that map to existing files
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]

# Rewrite EVERYTHING to the "pages" sub-subdirectory if not already
# eg. /pages/example/foo.php to /pages/example/pages/foo.php  
RewriteRule ^(?!pages/)(.*) pages/$1 [L]

(?!pages/) is a negative lookahead to avoid rewriting requests that are already for the /pages sub-subdirectory (inside the /pages/example directory).

But going to /example/pages/ in my browser shows the index.php instead of 404'ing as it should be trying to look for a folder called pages within the pages directory - but it seems no.

A request for /example/pages/ will now result in a 403 Forbidden as it will attempt to list the directory contents /pages/example/pages/. This is assuming Options -Indexes is set and you have no index.php file located at /pages/example/pages/index.php.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Webmasters Stack Exchange
适用范围
以原帖系统、软件版本与发布日期为准(2019-10-22)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。