简体   繁体   English

htaccess-将多个文件重定向到子文件夹到父文件夹

[英]htaccess - redirect multiples files into subfolders to parent folder

I am looking to rewrite the url, like this: 我正在寻找重写网址,如下所示:
From: 从:

http://localhost/system/view/mod001/m1_file_01.php (or any file inside mod001)
or 
http://localhost/system/view/mod002/m2_file_01.php (or any file inside mod002)

To

http://localhost/system/m?_file_01.php

I mean, hide/remove the view folder and the (mod001|mod002|mod003|etc) folder, and I have not gotten. 我的意思是,隐藏/删除视图文件夹和(mod001 | mod002 | mod003 | etc)文件夹,但我还没有得到。

This is my folders structure: 这是我的文件夹结构:

-system/
    .htaccess
    index.php
    view/
        mod001/
            m1_file_01.php | m1_file_02.php | ...
        mod002/
            m2_file_01.php | m2_file_02.php | ...
        media/
            css/ | js/ | ...

And this is my .htaccess 这是我的.htaccess

RewriteEngine On
#RewriteBase / #Yeah is commented
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . /system/ [L,R=301]

How I can do it? 我该怎么办?

Ok, let's imagine that you want to get m1_file_02.php and then you want to get m2_file_02.php How the server can guess forwhich exact file/folder you want to send request to? 好的,假设您要获取m1_file_02.php ,然后获取m2_file_02.php ,服务器如何猜测要向其发送请求的确切文件/文件夹? You have to specify an unique identificator for each folder. 您必须为每个文件夹指定一个唯一的标识符。 Anyway, http://localhost/system/m?_file_01.php it can't be using that structure you've given. 无论如何, http://localhost/system/m?_file_01.php它不能使用您提供的结构。

You would likely need a different rule for each of the 3 possible digits in modXXX because you can't really string pad with regular expressions. 对于modXXX中的3个可能的数字,您可能需要不同的规则,因为您不能真正使用正则表达式填充字符串。 Something like this might work, but I haven't tested it: 这样的事情可能会起作用,但我尚未对其进行测试:

#match 1 digit
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (m([0-9])_.*\.php) view/mod00$2/$1 [QSA,L]

#match 2 digits
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (m([0-9][0-9])_.*\.php) view/mod0$2/$1 [QSA,L]

#match 3 digits
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (m([0-9][0-9][0-9])_.*\.php) view/mod$2/$1 [QSA,L]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM