简体   繁体   中英

Rewriting WordPress theme files redirecting all content to index.php in httpd.conf

I am using WordPress on RHEL6, and have some rewrites so that CSS is hosted at http://servername/css rather than http://servername/wp-content/themes/themename/css/ .

I am moving from an Nginx install to Apache, and own the server. In that case, I don't want to use htaccess for redirects, I just want them in the httpd.conf. If I enable htaccess and put this in there, it all seems to work fine.

In my conversion, I added the / at the beginning of the rule. The current code below works for everything, except real files not handled by the css, js, img, font rules. As in, things that are actually hosted under /assets or other directories. I have a feeling this is due to the REQUEST_FILENAME, and if anything, a slash missing for the next rule - but I can't seem to figure it out.

RewriteRule ^/index\.php$ - [L]
RewriteRule ^/css/(.*) /wp-content/themes/themename/css/$1 [QSA,L]
RewriteRule ^/js/(.*) /wp-content/themes/themename/js/$1 [QSA,L]
RewriteRule ^/img/(.*) /wp-content/themes/themename/img/$1 [QSA,L]
RewriteRule ^/font/(.*) /wp-content/themes/themename/font/$1 [QSA,L]
RewriteRule ^/plugins/(.*) /wp-content/plugins/$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

If I try to access http://servername/assets/image.png , I actually am being rewritten to /index.php, from the rewrite log

init rewrite engine with requested uri /assets/311.jpg
applying pattern '^/index\.php$' to uri '/assets/311.jpg'
applying pattern '^/css/(.*)' to uri '/assets/311.jpg'
applying pattern '^/js/(.*)' to uri '/assets/311.jpg'
applying pattern '^/img/(.*)' to uri '/assets/311.jpg'
applying pattern '^/font/(.*)' to uri '/assets/311.jpg'
applying pattern '^/plugins/(.*)' to uri '/assets/311.jpg'
applying pattern '.' to uri '/assets/311.jpg'
RewriteCond: input='/assets/311.jpg' pattern='!-f' => matched
RewriteCond: input='/assets/311.jpg' pattern='!-d' => matched
rewrite '/assets/311.jpg' -> '/index.php'

/assets is a real folder that has real images. By looking at this log, it would make me think that !-f should have said 'yes, this is a file', rather than passing it to index.php afterwards.

After doing some research and testing, I found this was easily related to the difference in scope between htaccess and httpd.conf, and needing to include DOCUMENT_ROOT

If you are using htaccess in /var/www/html/test/, you will only be looking in /test, and therefore something like test/assets/image.jpg would pass, because /assets/image.jpg is valid.

In the case of httpd.conf though, unless you use these rules inside a Directory directive, you are not pathed to the virtual host folder. So in my case, it was looking for /assets/image.jpg off the root of the server.

To get around this, I just had to include %{DOCUMENT_ROOT} before %{REQUEST_FILENAME} .

My final block in httpd.conf looks like:

RewriteRule ^/index\.php$ - [L]
RewriteRule ^/css/(.*) /wp-content/themes/themename/css/$1 [QSA,L]
RewriteRule ^/js/(.*) /wp-content/themes/themename/js/$1 [QSA,L]
RewriteRule ^/img/(.*) /wp-content/themes/themename/img/$1 [QSA,L]
RewriteRule ^/font/(.*) /wp-content/themes/themename/font/$1 [QSA,L]
RewriteRule ^/plugins/(.*) /wp-content/plugins/$1 [QSA,L]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

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