简体   繁体   English

使用 .htaccess 从 URL 中删除子目录名称

[英]Remove sub-directory name from URL using .htaccess

I've installed Wordpress in root directory and CodeIgniter in sub directory.我已经在根目录中安装了Wordpress,在子目录中安装了CodeIgniter I'm having the following set of urls:我有以下一组网址:

  1. http://www.example.com/about-us (Wordpress) http://www.example.com/about-us (Wordpress)
  2. http://www.example.com/sub-dir/users/login (CodeIgniter) http://www.example.com/sub-dir/users/login (CodeIgniter)
  3. http://www.example.com/sub-dir/merchant/login (CodeIgniter) http://www.example.com/sub-dir/merchant/login (CodeIgniter)

Now I want to remove sub-dir only from second url and the remaining urls should work as they are.现在我只想从第二个url 中删除子目录,其余的 url 应该按原样工作。

Here is .htaccess code in root directory (Wordpress)这是根目录中的 .htaccess 代码(Wordpress)

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /yevma/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /yevma/index.php [L]
</IfModule>
# END WordPress

Any help would be appreciated!任何帮助,将不胜感激!

Your question is vague, we can only assume that you are trying to publish the resource currently reachable via that second URL under a different, shorter URL.您的问题含糊不清,我们只能假设您尝试在不同的较短 URL 下发布当前可通过第二个 URL 访问的资源。

Also assuming that this will not somehow collide with other resources this should point you into the right direction:还假设这不会以某种方式与其他资源发生冲突,这应该为您指明正确的方向:

RewriteEngine on
RewriteRule ^/?users/login$ /sub-dir/users/login [QSA,END]

Note however tht this might cause issues with the internal references used in those applications you have, since you break the structure with such a single redirection.但是请注意,这可能会导致您拥有的那些应用程序中使用的内部引用出现问题,因为您使用这样的单个重定向破坏了结构。

In case you receive an internal server error (http status 500) using the rule above then chances are that you operate a very old version of the apache http server.如果您使用上述规则收到内部服务器错误(http 状态 500),那么您可能运行的是非常旧版本的 apache http 服务器。 You will see a definite hint to an unsupported [END] flag in your http servers error log file in that case.在这种情况下,您将在 http 服务器错误日志文件中看到不支持的[END]标志的明确提示。 You can either try to upgrade or use the older [L] flag, it probably will work the same in this situation, though that depends a bit on your setup.您可以尝试升级或使用旧的[L]标志,在这种情况下它可能会起作用,尽管这有点取决于您的设置。

This implementation will work likewise in the http servers host configuration or inside a dynamic configuration file (".htaccess" file).此实现将在 http 服务器主机配置或动态配置文件(“.htaccess”文件)中类似地工作。 Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host.显然重写模块需要在http服务器中加载并在http主机中启用。 In case you use a dynamic configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.如果您使用动态配置文件,您需要注意在主机配置中完全启用它的解释,并且它位于主机的DOCUMENT_ROOT文件夹中。

And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using dynamic configuration files (".htaccess").还有一个一般性的评论:你应该总是喜欢将这样的规则放在 http 服务器主机配置中,而不是使用动态配置文件(“.htaccess”)。 Those dynamic configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server.这些动态配置文件增加了复杂性,通常是意外行为的原因,难以调试,而且它们确实会降低 http 服务器的速度。 They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).它们仅在您无法访问真正的 http 服务器主机配置(阅读:非常便宜的服务提供商)或坚持编写自己的规则的应用程序(这是一个明显的安全噩梦)的情况下作为最后一个选项提供。

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

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