简体   繁体   English

htaccess-如果url大于root,则将用户从index.php重定向到另一个文件

[英]htaccess - redirect user from index.php to another file if url is more than root

I have a need where I have 2 files located within the same directory 我需要在同一目录中有2个文件的地方

index.php and room.php index.php和room.php

What I want to be able to do it if a user goes to www.example.com it will direct them to index.php 如果用户访问www.example.com,我希望能够将其定向到index.php

If a user types in the url www.example.com/whateverelse/ it will redirect them to room.php 如果用户输入网址www.example.com/whateverelse/,则会将其重定向到room.php

Also I would like to capture the extra part of the URL into a variable for instance whateverelse 此外,我想捕捉URL的额外部分转化为实例变量whateverelse

Is this even possible? 这有可能吗?

Yes it is very much possible. 是的,这很有可能。 Use this code in your root .htaccess: 在您的根.htaccess中使用以下代码:

# load index.php by default
DirectoryIndex index.php

RewriteEngine On
RewriteBase /

# remove trailing slash    
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ /$1 [NE,R=301,L]

# for all other requests load room.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^((?!(index|room)\.php).+)$ room.php [L,NC]

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

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