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

Use .htaccess to remove a directory and .php extension from the URL

· 52CE 我爱测

I am running a Wordpress website, but I have some folders not related to the wordpress CMS, but in the same directory where all Wordpress folders are. The .htaccess in root WP folder has its standard script: RewriteEngine On Rewr…

问题原文

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

I am running a Wordpress website, but I have some folders not related to the wordpress CMS, but in the same directory where all Wordpress folders are.

The .htaccess in root WP folder has its standard script:

RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

Everything what is related to Wordpress CMS is working perfectly. Now I have that other folder named companies, inside that folder there is a lot of other sub-directories with the company names. There will be more folders created every day, so the current URL for this problem is:

www.example.com/companies/company-name/employee.php

and my goal for this is to have

www.example.com/company-name/employee

(also removing the .php extension).

How to achieve this?

I have tried modifying .htaccess in the root folder, the best result was that I could get my desired link to work, but then the whole WordPress site and all images would not be loaded.

Should I create a new .htaccess inside companies folder? I would not mind creating a new .htaccess file every time in new the folder (I am that desperate to get this working).

已采纳解答

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

Should I create a new .htaccess inside "companies" folder?

No. In order to hide the /companies directory from the URL you are going to have to modify the .htaccess file in the parent directory. ie. the WordPress .htaccess file in the document root.

There is CSS file and few images inside /companies

So I assume you intend to remove /companies from all URLs - not just the "employee" URL mentioned.

In order to avoid conflicts with WordPress, you first need to test whether the resource being requested exists in the /companies directory. If so, rewrite the request. Otherwise, let the request fall through to WordPress.

Try something like the following, before the existing WordPress directives, in the root .htaccess file:

Options -MultiViews

# Any requests for /companies directly are ignored
# This includes rewritten requests (below)
RewriteRule ^companies/ - [L]

# Check if the requested resource exists in the /companies subdirectory.
# If so, rewrite the request to that subdirectory.
RewriteCond %{DOCUMENT_ROOT}/companies/$1 -f
RewriteRule (.+\.(css|js|png|jpg|gif))$ /companies/$1 [L]

# Check if the requested PHP file exists in the /companies subdirectory
# if requesting a URL that does not already have a file extension.
# NB: Assumes the URL-path does not otherwise contain dots.
# If so, rewrite the request to that subdirectory appending ".php"
RewriteCond %{DOCUMENT_ROOT}/companies/$1.php -f
RewriteRule ^([^.]+)$ /companies/$1.php [L]

# BEGIN WordPress
# :

Note, that this doesn't actually remove the /companies subdirectory (or .php extension) from the URL (as suggested in the modified question title) - for that you need to modify the actual links to these files in your HTML source.

This allows you to access the pages/resources without having to specify the /companies directory in the URL.

UPDATE: Is it possible to do redirection from this original one to the new one?

Yes, modify the first rule:

# Any requests for /companies directly are ignored
# This includes rewritten requests (below)
RewriteRule ^companies/ - [L]

To the following instead:

# Redirect any "direct" requests to the `/companies` directory
# to remove the directory from the URL-path.
# Also removes the ".php" extension (if present)
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^companies(/.*?)(?:\.php)?$ $1 [R=302,L]

The regex capturing subpattern (/.*?) is non-greedy (denoted by the ?), this is to avoid capturing the optional .php file extension.

The condition (RewriteCond directive) that checks against the REDIRECT_STATUS environment variable ensures that we only check direct requests and not rewritten requests by the directives below. This avoids a redirect loop. The REDIRECT_STATUS env var is not set on the initial request and is set to the HTTP status code (ie. 200) after the first successful rewrite.

This redirect is only really required if the old URLs have already been indexed by search engines and/or linked to by external 3rd parties. This does not avoid having to replace the old URLs in the application.

Note also, that this is currently a temporary (302) redirect. Only change this to a permanent (301) redirect once you have confirmed it works OK.

Summary

Bringing to the two parts together:

Options -MultiViews

# Redirect any "direct" requests to the `/companies` directory
# to remove the directory from the URL-path.
# Also removes the ".php" extension (if present)
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^companies(/.*?)(?:\.php)?$ $1 [R=301,L]

# Check if the requested resource exists in the /companies subdirectory.
# If so, rewrite the request to that subdirectory.
RewriteCond %{DOCUMENT_ROOT}/companies/$1 -f
RewriteRule (.+\.(css|js|png|jpg|gif))$ /companies/$1 [L]

# Check if the requested PHP file exists in the /companies subdirectory
# if requesting a URL that does not already have a file extension.
# NB: Assumes the URL-path does not otherwise contain dots.
# If so, rewrite the request to that subdirectory appending ".php"
RewriteCond %{DOCUMENT_ROOT}/companies/$1.php -f
RewriteRule ^([^.]+)$ /companies/$1.php [L]

# BEGIN WordPress
# :

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

资料来源与适用范围

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

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

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