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
# :
/companies
? Or do you only have URLs of the form/company-name/employee
that should reference this filesystem path?