简体   繁体   English

将一级子目录重写为根目录传递文件夹名称.htaccess

[英]Rewrite first level subdirectory to root directory passing folder name .htaccess

I have an application where I pass in the customer name to determine which information to show on the page.我有一个应用程序,我在其中传递客户名称以确定要在页面上显示哪些信息。

Previously I had it setup to be subdomain based, for example https://client1.company.com , https://client2.company.com .以前我将它设置为基于子域,例如https://client1.company.comhttps://client2.company.com I would parse the subdomain to determine the customer name, and each subdomain pointed to the same set of files on the server.我会解析子域以确定客户名称,并且每个子域都指向服务器上的同一组文件。

I have to change this to be subdirectory based, so it would now be https://company.com/client1/ , https://company.com/client2/ , etc.我必须将其更改为基于子目录,因此现在将是https://company.com/client1/https://company.com/client2/等。

Is there an .htaccess rewrite rule I can use to basically rewrite any request with a subdirectory back to the root directory?是否有一个 .htaccess 重写规则可以用来基本上将带有子目录的任何请求重写回根目录? This way when an ajax request goes to the server from https://company.com/client2/ it will redirect the request to https://company.com/ajax.php as opposed to sending the request to https://company.com/client2/ajax.php and it not being found.这样,当 ajax 请求从https://company.com/client2/发送到服务器时,它会将请求重定向到https://company.com/ajax.php ,而不是将请求发送到https://company.com/client2/ajax.php并且没有找到。

When loading stylesheets, etc. it would redirect https://company.com/client1/assets/css/style.css to https://company.com/assets/css/style.css .当加载样式表等时,它会将https://company.com/client1/assets/css/style.css重定向到https://company.com/assets/css/style.css

You may use this rule in your site root .htaccess:您可以在站点根目录 .htaccess 中使用此规则:

RewriteEngine On

# redirection to a level up
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^[\w-]+(/.*)$ $1 [L,R=301]

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

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