简体   繁体   中英

Rewrite index and one folder to static pages with WordPress rewrites on the other

I'm looking to have all WordPress rewrites to work except:

  • Home page as a static html page, content is in index.html in the root folder
  • /business be routed to a business folder in the root dir which contains static html pages.

When I set the index to . /index.html [L] . /index.html [L] none of the regular wordpress rewrites work. However if I set DirectoryIndex /index.html I can't figure out how to get rewrites to work for /business which contains HTML files that need to be served up on the http://mywebsite.com/business url. Full rewrite rules:

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

Any help is appreciated and, more than just an answer, an explanation of what each portion of the lines you provide does may help myself and others understand how to take this on on their own next time.

UPDATE: Rules are still not working. /wp-admin has worked all along though.

Try to stick with default WordPress rewrites and simply add DirectoryIndex to prefer .html files over .php files:

DirectoryIndex index.html index.php

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

So rewrites will only happen when requested files or directories are missing (RewriteConds with !-f and !-d flags). And if there is a static index.html along with index.php in a directory (root directory for example), it will be served first.

http://httpd.apache.org/docs/2.2/mod/mod_dir.html#directoryindex

Try:

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

Replace this code with your HTACCESS file in root

# Switch rewrite engine off in case this was installed under HostPay.
RewriteEngine Off

SetEnv DEFAULT_PHP_VERSION 53

DirectoryIndex index.cgi index.php

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

And also change the URL's from database...

Two issues need to be solved:

  1. Serve a particular HTML file whenever someone needs the front page.
  2. Serve static files from a static directory alongside WordPress.

To solve the first issue, I would rename index.html to front-page.php and move it inside my current theme folder. Then WordPress would serve it (or rather: use it as a template) whenever someone requests the front page, according to the Template Hierarchy .

There is a cost to this solution compared to actually serving a static file: Whenever someone requests you front page, WordPress will still be loaded. If the goal of serving your front page statically is to save server resources by not loading WordPress or PHP on every page load, you should look into caching plugins.

The second issue should not be an issue at all, because the standard WordPress rewrite rules already allow for static files and directories. The lines

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

mean that the redirect to WordPress won't happen if the URL requested is an actual file (-f) or directory (-d). Therefore you will not need any extra rewrite rules to access the /business directory.

The standard WordPress rewrite rules are explained below:

RewriteEngine On                    # Turn on the rewrite engine
RewriteBase /                       # Use the domain root as the base of rewrites
RewriteRule ^index\.php$ - [L]      # If someone requests the WordPress entry point, stop here
RewriteCond %{REQUEST_FILENAME} !-f # If the requested URL is not an actual file ...
RewriteCond %{REQUEST_FILENAME} !-d # ... and if the requested URL is not an actual directory ...
RewriteRule . /index.php [L]        # ... then rewrite it to the main WordPress entry point

When /index.php is loaded, this file will then in turn load WordPress and everything that comes with it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM