admin管理员组文章数量:1332322
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
, example/foo
or example/foo/bar
, the default Wordpress website should load. and when I request any url that starts with the servers
folder: example/servers
, example/servers/foo
, example/servers/foo/bar
, I want the example/com/servers/index.php
file to be executed.
The current example/servers/.htaccess
file only executes this url example/servers
with the example/servers/index.php
file but it's not executing other urls that start with the servers
folder(example/servers/foo
, example/servers/foo/bar
) by the example/servers/index.php
file.
How can edit the htaccess file so that all request to example/servers/*
are executed by the example/servers/index.php
file
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
, example/foo
or example/foo/bar
, the default Wordpress website should load. and when I request any url that starts with the servers
folder: example/servers
, example/servers/foo
, example/servers/foo/bar
, I want the example/com/servers/index.php
file to be executed.
The current example/servers/.htaccess
file only executes this url example/servers
with the example/servers/index.php
file but it's not executing other urls that start with the servers
folder(example/servers/foo
, example/servers/foo/bar
) by the example/servers/index.php
file.
How can edit the htaccess file so that all request to example/servers/*
are executed by the example/servers/index.php
file
2 Answers
Reset to default 1To 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:
- 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
- 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.
# 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 toindex.php
(relative path) in the current directory anyway (default behaviour).Or, explicitly set
RewriteBase /server
. (But that is mostly redundant.)
本文标签: redirectHow to forward all requests starting with a specific folder name to the same folder
版权声明:本文标题:redirect - How to forward all requests starting with a specific folder name to the same folder 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742271895a2444460.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论