2

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).

3
  • "I have tried modifying .htaccess in the root folder" - Please include what you have tried. Do you have other resources (JS, CSS, images) inside /companies? Or do you only have URLs of the form /company-name/employee that should reference this filesystem path?
    – MrWhite
    Nov 21, 2019 at 11:19
  • There is CSS file and few images inside /companies.The CSS and images are for the PHP files inside /company-name folder
    – Newbie187
    Nov 21, 2019 at 11:44
  • Addition to that comment, I could get rid of any files in /companies folder and place them somehwere else, if that would help to reach the solution to the problem easier
    – Newbie187
    Nov 21, 2019 at 12:49

1 Answer 1

3

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
# :
2
  • Incredible! More importantly, with all comments... Now I can access the required URL from the original link (www.example.com/companies/company-name/employee.php) and the new one (www.example.com/company-name/employee). Is it possible to do redirection from this original one to the new one?
    – Newbie187
    Nov 21, 2019 at 13:22
  • 1
    Yes, it's possible to redirect the "old" URLs if required. I've updated my answer.
    – MrWhite
    Nov 21, 2019 at 15:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.