Forum Moderators: phranque

Message Too Old, No Replies

Rewrite everything but images/css

in wordpress

         

wesg

3:11 am on Feb 5, 2008 (gmt 0)

10+ Year Member



I am in the middle of a challenging project changing my Wordpress installation that resides in a subfolder to appear in the root, without actually moving anything.

I have made progress, but I ran into a problem when I set up 301 redirects from any URL with /blog/ in it.

How can I redirect all permalinks with /blog/ in them to the root, but leave the requests for things that are actual files alone? (Anything with the extension of php, png, gif, jpg or css must be ignored). When I set it up, I broke my image/css folder structure and my site appeared blank.

Any help is appreciated.

wesg

3:23 am on Feb 5, 2008 (gmt 0)

10+ Year Member



Actually, it's better to have it redirected only if the file requested doesn't exist. How can that be done?

jdMorgan

3:39 am on Feb 5, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Actually, it's far more efficient to do both: Exclude "known" filetypes and then check for the existence only of the "unknown" filetypes -- "File exists" checks are thousands of time more expensive in CPU cycles than simple pattern-based filetype checking within a rule.

Please post your best-effort code for discussion [webmasterworld.com].

Also, be aware that it is the client (e.g. browser) that resolves relative links based up the lowest-level directory that it "sees" in its address bar. If you are rewriting the URL requested by the client and your pages use page-relative links for included objects (CSS, images, external JS, etc.), then you will likely encounter the common problem of broken object links.

The easiest solution is to use server-relative ( e.g. <img src="/images/logo.gif"> ) or canonical ( e.g. <img src="http://example.com/images/logo.gif"> ) links for included objects.

A rewriting solution is also possible, but incurs a duplicate-content problem (it "creates" more than one URL for a single resource).

Jim

wesg

3:54 am on Feb 5, 2008 (gmt 0)

10+ Year Member



In addition to having Wordpress use the new blog location, I have this code to redirect the pages containing /blog/ to the root location.

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME}!-f
RewriteCond %{REQUEST_FILENAME}!-d
RewriteRule ^blog/?(.*) /$1 [R=301,L]
</IfModule>

Now my problem is that it rewrites the blog admin page as well. How can I exclude rewrites with a specific phrase? ie. /wp-admin/

jdMorgan

4:57 am on Feb 5, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Your code as posted will only redirect URL-paths starting with "/blog".

Does the path to /wp-admin/ also start with "/blog"? If so, add another RewriteCond at the top of the others:


RewriteCond %{REQUEST_URI} !^/blog/wp-admin/

or equivalently, with your current RewriteRule pattern:

RewriteCond $1 !^wp-admin/

You can read the first example as "If requested URI does NOT start with /blog/wp-admin/"

You can also dump the <IfModule> container if you would prefer to get an error message instead of a silent failure in the case that mod_rewrite is not installed and loaded on your server.

Jim

wesg

2:05 pm on Feb 5, 2008 (gmt 0)

10+ Year Member



I found a solution.

I moved the Wordpress installation up to the root folder, and 301 redirected all requests for files with /blog/. This way my image linnks aren't broken, and Google will retain my previous rankings.