Forum Moderators: phranque
I've got a wordpress site that has a url
www.mysite.com/product
I am coding a custom section for the site that integrates into an order system i've created seperately, but what I need the code to do is redirect properly.
www.mysite.com/order/product/ -> should go to www.mysite.com/order/product/, but it instead goes to www.mysite.com/product/ because that product is within the wordpress setup - it automatically searches for it.
I've been lead to belive that it's to do with .htaccess
My current .htaccess is standard wp -
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
I need something like the following - but it 500 errors because I don't know enough about syntax etc to be able to achieve desired outcome
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond /order/${REQUEST_FILENAME} !-f
RewriteCond /order/${REQUEST_FILENAME} !-d
RewriteRule . /order/ [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Essentially,
IF URL CONTAINS "ORDER"
- REDIRECT TO /ORDER/<url>
ELSE
- CONTINUE NORMALLY
END IF
There isn't an "order" directory, it's simply a page within wordpress that handles all the code I require. So i'm not sure if that has any affect on what is required.
Thanks for all your insight, there's probably a simple function or solution, but I just don't know where to start, and the search hasn't come up with anything similar.
First, this is not a "redirect" it is an internal rewrite. You'll see that many contributors here will use the somewhat-redundant terms "external redirect" and "internal rewrite" to help to clarify this distinction.
An external redirect sends an immediate HTTP response to the requesting client (e.g. a browser or search engine robot) that says, "The resource you requested has moved. Please ask for it again at this new URL." This terminates the current HTTP transaction, and the client must begin a new HTTP transaction re-requesting the resource it wanted using the new URL provided in the server's redirect response. Thus, a redirect is a URL-to-URL translation, and the client is involved.
On the other hand, an internal rewrite occurs entirely within the context of the current HTTP transaction, and is a URL-to-filename translation function. The client is not notified that the URL-to-filepath mapping is being changed from the default mapping that would occur in the absence of any action by mod_rewrite, mod_dir, mod_alias, or mod_proxy.
When dealing with mod_rewrite to invoke internal rewrites or external redirects, precise thinking is required. Just as an example, you stated that you wanted to "redirect if the requested URL-path *contains* "ORDER". But that's not correct, as what you really need to do is to *rewrite* if the requested URL-path *starts with* "/order/". This distinction may appear to be subtle, but it's the difference between proper server operation and intermittent errors. Note that "starts with" and "contains" can be very different things, and that both Apache and mod_rewrite are case-sensitive.
All that said, your problem appears to be a very simple one. Simply detect client URL-path requests that start with "/order/" and exclude them from the WP rewrite. Getting rid of unnecessary/superfluous directives, your .htaccess code would change from
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
RewriteEngine on
#
# BEGIN WordPress
#
RewriteCond %{REQUEST_URI} !^/order/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
Having addressed this immediate problem, you may wish to look into the various domain and URL canonicalization subjects which are frequently discussed here in this forum and in the Google News forum; Heading off canonicalization problems and the resulting duplicate-content issues before they develop is a good practice. The goal is that for each resource on your site (page, image, etc.), one and only one unique URL should be able to access that resource; Any variation whatsoever on that canonical URL should result in an external 301-Moved Permanently redirect to the "correct" canonical URL for that resource, or a 404-Not Found response. Typically, you'll want to address
Jim