0

In my Wordpress website main directory, I created a folder servers, which contains two files .htaccess & index.php.

In the .htaccess file, I add those lines to redirect all requests to that folder to the index.php file.

# Enable rewriting.
RewriteEngine on

# Optional: do not allow perusal of directories.
Options -Indexes

# Optional: explicitly enable per-directory rewrites in the .htaccess context.
Options +FollowSymLinks

# Required when not in the webroot. Always use a trailing slash.
RewriteBase /

# To be able to access existing directories and files (standalone scripts).
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f

# Redirect everything else to index.php.
# Add QSA to ensure that querystring variables are registered as such.
RewriteRule . index.php [L,QSA]

# php -- BEGIN cPanel-generated handler, do not edit
# Set the “ea-php72” package as the default “PHP” programming language.
<IfModule mime_module>
  AddHandler application/x-httpd-ea-php72 .php .php7 .phtml
</IfModule>
# php -- END cPanel-generated handler, do not edit

So whenever a user requests a link like example.com, example.com/foo or example.com/foo/bar, the default Wordpress website should load. and when I request any url that starts with the servers folder: example.com/servers, example.com/servers/foo, example.com/servers/foo/bar, I want the example/com/servers/index.php file to be executed.

The current example.com/servers/.htaccess file only executes this url example.com/servers with the example.com/servers/index.php file but it's not executing other urls that start with the servers folder(example.com/servers/foo, example.com/servers/foo/bar) by the example.com/servers/index.php file.

How can edit the htaccess file so that all request to example.com/servers/* are executed by the example.com/servers/index.php file

2 Answers 2

1

To achieve this you don't need the .htaccess in the servers directory itself. You can remove this and apache can use rules in the root Wordpress .htaccess to do the rewrite you need, and you can usefully keep rewrite rules for your site in one place.

You need a couple of rules to make this work:

RewriteRule ^servers/index.php - [L]
RewriteRule ^servers/(.*) servers/index.php?page=$1 [L]

These say:

  1. If we're already requesting a page that starts with servers/index.php then quit process rewrite rules. This is there to stop redirect loop errors
  2. Any pages that start with servers/, redirect to servers/index.php, passing everything that was originally after servers/ to a parameter in index.php if you need it. E.g. this will send servers/foo/bar to servers/index.php?page=foo/bar

Edit: You need to place these rules before any other RewriteRules or RewriteCond statements so that they get processed first.

1
# Required when not in the webroot. Always use a trailing slash.
RewriteBase /

Your RewriteBase / directive is resulting in all requests (except /server/ itself*1) being rewritten back to the document root. ie. /index.php, not /server/index.php as intended.

Aside: I would add, that comment is simply not true. It depends what you are doing. In fact, more often than not, the RewriteBase directive should be omitted entirely (as is the case here).

(*1 Requests for /server/ "work" because the regex . on the RewriteRule directive fails and the request is then left for mod_dir to issue an internal subrequest for index.php - the DirectoryIndex - in the current directory.)

You need to either:

  • Remove this RewriteBase directive altogether - since you are rewriting to index.php (relative path) in the current directory anyway (default behaviour).

  • Or, explicitly set RewriteBase /server. (But that is mostly redundant.)

Your Answer

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

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