homepage Welcome to WebmasterWorld Guest from 54.161.240.10
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

This 41 message thread spans 2 pages: < < 41 ( 1 [2]     
Simple 301 redirect and Wordpress htaccess rules
agneslesage




msg:4182514
 3:04 pm on Aug 5, 2010 (gmt 0)

Hi

I am moving a site that was in ASP into Wordpress, whose friendly URL I truly enjoy :-)

Though, I have troubles to get my 301 redirects work...

I ve checked useful help on post
[webmasterworld.com...]
And tried to work and test around different things, though, without success. So I hope jdMorgan reads this as he seems to be the master :)

My Wordpress site has this isntructions in .htaccess, and if I remove them the site does not work (except the homepage), I guess it is used to handle the friendly URL:

# 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


Now, I am trying to add a list of 301 redirects like:

Redirect 301 /template.asp?page=qui_nous&titre=qui_nous http://www.mysite.com/qui-sommes-nous/

I've tried put it before, inside, somewhere into the exwisting WP code... the instruction just seems to be ignored.

Any magic idea?

Thanks
Agnes

 

jdMorgan




msg:4189279
 2:43 pm on Aug 19, 2010 (gmt 0)

Add a negative-match rewritecond to the rule, excluding the URLs that correspond to the "WP admin menu of the e-commerce plugin (for editing categories)".

To work with mod_rewrite, almost everything must be defined in terms of URLs. After that, it is a simple matter of adding RewriteConds like:
RewriteCond %{REQUEST_URI} !^/url-path-of-page-that-I-do-not-want-to-rewrite-or-redirect.html$
-or-
RewriteCond %{REQUEST_URI} !^/url-path-of directroy-that-I-do-not-want-to-rewrite-or-redirect/

to stop the rule from being applied to those URLs.

Jim

agneslesage




msg:4194314
 7:29 am on Aug 31, 2010 (gmt 0)

Hi Jim
I should rename this post "complicate 301 redirect", no...?
Or "it is not so easy to become an expert"!

I am trying to put in this exception for wp-admin (.../wp-admin/), but I dont know what I am doing wrong it does not apply... May be I am not allowed to put 2 conditions? Or should concatenate them somehow?
Here is what I am trying with

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


Insode this block:

# Redirect direct client requests for URL-path /index.php to / and for /folder/index.php to /folder/
# Likewise for default.php direct requests in root or in folder,
# With exception to avoid mess up in the admin
RewriteCond %{REQUEST_URI} !^/wp-admin/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*(index|default)\.(php|html?|asp)([?#][^\ ]*)?\ HTTP/
RewriteRule ^(([^/]+/)*)(index|default)\.(php|html?|asp)$ http://www.atable.com/$1 [R=301,L]

jdMorgan




msg:4194461
 11:58 am on Aug 31, 2010 (gmt 0)

Your modified code is correct. If it "does not work," then the problem is either:

1) You modified the wrong rule, and this is not the rule that is causing the problem.
-or-
2) You did not delete your browser cache before testing the new rule, and your browser is showing you stale locally-cached pages and server responses.

Instead of posting "it does not work," please give us more useful information:
1) How did you test (what URL did you type or click)?
2) What did you expect to happen?
3) What were the actual results?
4) How did the actual results differ from your expectations?

By *discussing* the problem you encountered, you give us a better chance of helping you fix the problem. Otherwise, this is like taking your car to the mechanic and saying, "It is broken, please fix it." Your mechanic may spend quite a large amount of time fixing the wrong problem! :o

Jim

agneslesage




msg:4194481
 12:32 pm on Aug 31, 2010 (gmt 0)

NB: see ps below: it "does not work" on my local server but does online so... never mind.

Sorry, sorry, I thought I did a more basic mistake that could be seen from the code...

Now, I am testing on my local server

http://localhost/www.atable-new.com/

where .htaccess is at the root, and I had the code:

# Redirect direct client requests for URL-path /index.php to / and for /folder/index.php to /folder/
# Likewise for default.php direct requests in root or in folder,
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*(index|default)\.(php|html?|asp)([?#][^\ ]*)?\ HTTP/
RewriteRule ^(([^/]+/)*)(index|default)\.(php|html?|asp)$ http://localhost/www.atable-new.com/$1 [R=301,L]


When I insert this code (among the other one you provided me above) into my .htaccess, everythink works fine in the front office, but there is a particular function of the back office which is troubled.
I use e-commerce plugin and go in wp-admin in the plugins section, to edit products categories, that is
http://localhost/www.atable-new.com/wp-admin/admin.php?page=wpsc-edit-groups

When I click on "edit" link of a category, it normally does
<a href='#' onclick='return showedit_categorisation_form()'> and opens the category edit form on the right lower part of the web page (same URL).
With the above lines of code redirection, unfortunately, the link prompts WP admin control panel to open in that space.
If I coment those 2 lines, it starts to functon properly again.

When I add the condition, ie

# Redirect direct client requests for URL-path /index.php to / and for /folder/index.php to /folder/
# Likewise for default.php direct requests in root or in folder,
# This rule customized to avoid mess up in the admin
RewriteCond %{REQUEST_URI} !^/wp-admin/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*(index|default)\.(php|html?|asp)([?#][^\ ]*)?\ HTTP/
RewriteRule ^(([^/]+/)*)(index|default)\.(php|html?|asp)$ http://localhost/www.atable-new.com/$1 [R=301,L]


It creates the same problem: the control panel loads in that space.
I think I did a good refresh though.... Because when I comment all it works, when I uncomment it makes the mistake.

I don't understand...

PS: OUPS!
Now I am trying the same online (where my other htaccess rules are slightly different because the ones online don't function properly on my localhost), and it works fine with this new condition!
Well, I wont understand why, but that is fine... Sorry to have bothered you.

jdMorgan




msg:4195048
 2:50 pm on Sep 1, 2010 (gmt 0)

No bother. This just illustrates the difficulty we have here of trying to get *all* the information needed to provide good answers -- Server type, URL-space layout, file/directory layout, subdomain-creation method used (e.g. Control Panel versus "manual/.htaccess"), content-negotiation, "applications" in use (e.g. WordPress, etc.) -- all of these factors can affect the proper solution...

This is not to mention the need for good "problem reports" that include the specific URL that is causing a problem, the expected results of requesting that URL, the actual results of requesting it, and a description of how the actual results differed from the expected results... It's really hard to figure out some problem without all of the above information.

In this case then, it seems that the problem was that the test environment differs from the production environment. Over the long term, you may wish to address that problem so that when you test on your local test machine, you can be sure that the tested code (whether .htaccess or scripts) will work on your 'live' server.

Jim

agneslesage




msg:4234479
 2:18 pm on Nov 24, 2010 (gmt 0)

Hello Jim!

Are you still there?
Me again!

I am coming back to my htaccess as I would like to simplify it (and still have a few new rules to add!)

I am bumoing into one difficulty - andf is exactly one you point out in your last message): I can't get my htaccess to work on my localhost. It is annoying, as testing on line on htaccess is not really recommended!

I tried to come back to the basics and can't sort it out nor find an answer on the web..

I am on:
- easyphp 5.3.1.0 with Apache server, all otions I think are right:
- allowoverrideall on my web site,
- LoadModule rewrite_module modules/mod_rewrite.so
- My web site is on [localhost...]
- I have permalinks neabled on WP 3.01: /%postname%/

Now:

- When I use the defaul WP generated htaccess, all is fine. It defines my rewritebase as /www.mysite.com/

- When I use my current, complicate one, or even the "JDMorgan optimised" simple one, I get 404 errors all around
And this, whether I comment out the rewritebase or I put /www.mysite.com/

# BEGIN WordPress
RewriteEngine on
# Unless you have set a different RewriteBase preceding this point,
# you may delete or comment-out the following RewriteBase directive:
RewriteBase /www.mysite.com/
# if this request is for "/" or has already been rewritten to WP
RewriteCond $1 ^(index\.php)?$ [OR]
# or if request is for image, css, or js file
RewriteCond $1 \.(gif|jpg|jpeg|png|css|js|ico)$ [NC,OR]
# or if URL resolves to existing file
RewriteCond %{REQUEST_FILENAME} -f [OR]
# or if URL resolves to existing directory
RewriteCond %{REQUEST_FILENAME} -d
# then skip the rewrite to WP
RewriteRule ^(.*)$ - [S=1]
# else rewrite the request to WP
RewriteRule . /index.php [L]
# END WordPress

There must be a reason?
Thanks
Agnes

agneslesage




msg:4234619
 7:02 pm on Nov 24, 2010 (gmt 0)

Hello
Here is the answer, provided by Dardna who also made a plugin using your code

[dardna.com...]

RewriteEngine on
# Unless you have set a different RewriteBase preceding this point,
# you may delete or comment-out the following RewriteBase directive:
RewriteBase /www.mysite.com/
# if this request is for "/" or has already been rewritten to WP
RewriteCond $1 ^(index\.php)?$ [OR]
# or if request is for image, css, or js file
RewriteCond $1 \.(gif|jpg|jpeg|png|css|js|ico)$ [NC,OR]
# or if URL resolves to existing file
RewriteCond %{REQUEST_FILENAME} -f [OR]
# or if URL resolves to existing directory
RewriteCond %{REQUEST_FILENAME} -d
# then skip the rewrite to WP
RewriteRule ^(.*)$ - [S=1]
# else rewrite the request to WP
RewriteRule . /www.mysite.com/index.php [L]
# END WordPress

This now works!

I guess I only have to see how to refusion everything now...

2 little questions though:

- I want to insert a few redirect rules for specific files or directories - where should I do that?
Just after this line?

RewriteRule ^index\.php$ - [L]

NB: I will simplify the former rules with a single redirect for my old web site whose all files started with either default.asp or template.asp somehtings
RewriteRule ^default\.asp$ [localhost...] [R=301,L]
RewriteRule ^template [localhost...] [R=301,L]
NB: this probably may even been done saying "anything.asp followed by anything"...?
This leads me to my second question

2> What is the best string for saying "any URL containing"...

You told me when it is in the string:
Query string contains name/value pair "page=qui_nous" : ^([^&]*&)*page=qui_nous(&.*)?$
Nopw for what is in a URL, is it the same?
I ghet some error 500 when I do

# RewriteRule ^([^&]*&)*specialites-italiennes((/[^/]+)*)/?$ [localhost...] [R=301,L]

Ideally, I'd like to say
"any URL containing X" redirects to "www.mysite.com/Y"
"and if it contains X followed by Z" redirects to www.mysite.com/Y/Z including any attached string"

You see what I mean?
(talking chinese, no!)

Agnes

g1smd




msg:4234689
 10:22 pm on Nov 24, 2010 (gmt 0)

All redirects MUST be placed before the start of the rewrites.

agneslesage




msg:4234780
 5:47 am on Nov 25, 2010 (gmt 0)

In fact, there is another question I wonder...

Is the code you provided me above (at the beginning of this post) meant to accomodate my complicate former requests? Or is it also more elaborate than the "JimMorgan code" people usually take as reference, which you provided in this forum some longer time ago?

Your code Dardna and other took as reference:

RewriteEngine on
# Unless you have set a different RewriteBase preceding this point,
# you may delete or comment-out the following RewriteBase directive:
RewriteBase /localhost/www.atable-new.com/
# if this request is for "/" or has already been rewritten to WP
RewriteCond $1 ^(index\.php)?$ [OR]
# or if request is for image, css, or js file
RewriteCond $1 \.(gif|jpg|jpeg|png|css|js|ico)$ [NC,OR]
# or if URL resolves to existing file
RewriteCond %{REQUEST_FILENAME} -f [OR]
# or if URL resolves to existing directory
RewriteCond %{REQUEST_FILENAME} -d
# then skip the rewrite to WP
RewriteRule ^(.*)$ - [S=1]
# else rewrite the request to WP
RewriteRule . /index.php [L]
# END WordPress


The code you provided me here (before we complicated further):

RewriteEngine On
RewriteBase /
#
# (here came some specific redirect rules)
#
# Redirect direct client requests for URL-path /index.php to / to avoid duplicate content
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /index\.php([?#][^\ ]*)?\ HTTP/
RewriteRule ^index\.php$ http://www.example.com/ [R=301,L]
#
# Redirect requests for non-blank, non-canonical hostnames to canonical hostname
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
#
# BEGIN WordPress
# Except for requests for /index.php and for the most-frequently-requested
# filetypes that WP cannot generate, rewrite all URL requests which do not
# resolve to an existing file or directory to the WordPress script filepath
RewriteCond $1 !^index\.php$
RewriteCond $1 !\.(gif|jpe?g|png|ico|css|js)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ /index.php [L]
# END WordPress


If this latter one is more advanced (as I suspect), then how should I adapt for my rewritebase to work?
I am trying to replicate the same trick to change the localbase... but I get an error message even on the Homepage ("the page is not redirected properly - FF detects that the server redirects the request for this page in a manner that wont succeed"


RewriteEngine On
RewriteBase /
#
# (here came some specific redirect rules)
#
# Redirect direct client requests for URL-path /index.php to / to avoid duplicate content
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /index\.php([?#][^\ ]*)?\ HTTP/
RewriteRule ^index\.php$ http://localhost/www.atable-new.com/ [R=301,L]
#
# Redirect requests for non-blank, non-canonical hostnames to canonical hostname
RewriteCond %{HTTP_HOST} !^(www\.atable-new\.com)?$
RewriteRule ^(.*)$ http://localhost/www.atale-new.com/$1 [R=301,L]
#
# BEGIN WordPress
# Except for requests for /index.php and for the most-frequently-requested
# filetypes that WP cannot generate, rewrite all URL requests which do not
# resolve to an existing file or directory to the WordPress script filepath
RewriteCond $1 !^index\.php$
RewriteCond $1 !\.(gif|jpe?g|png|ico|css|js)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ /atable-new/index.php [L]
# END WordPress


Thyanks for your help

Agnes

agneslesage




msg:4234791
 6:39 am on Nov 25, 2010 (gmt 0)

Hello

Ok, I think I managed to get together Jim Morgan's "normal" rules and the addiotional ones provided in this post, and to get it all to work locally, with the following code.

Other questions I'll get on another post!


RewriteEngine on

# REWRITEBASE

# may be online (www.atable.com) or local (http://localhost/www-atable-new.com)
### local
RewriteBase /www.atable-new.com/
### online
## RewriteBase /


# ADDITIONAL JIM MORGAN RULES:

# Redirect direct client requests for URL-path /index.php to / to avoid duplicate content
# with the exception of wp-admin directory
### local
RewriteCond %{REQUEST_URI} !^/www.atable-new.com/wp-admin/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*(index|default)\.(php|html?|asp)([?#][^\ ]*)?\ HTTP/
RewriteRule ^(([^/]+/)*)(index|default)\.(php|html?|asp)$ http://localhost/www.atable-new.com/$1 [R=301,L]
### online
## RewriteCond %{REQUEST_URI} !^/wp-admin/
## RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*(index|default)\.(php|html?|asp)([?#][^\ ]*)?\ HTTP/
## RewriteRule ^(([^/]+/)*)(index|default)\.(php|html?|asp)$ http://www.atable.com/$1 [R=301,L]

# Redirect requests for non-blank, non-canonical hostnames to canonical hostname
### local - I don't get it to work
### online
## RewriteCond %{HTTP_HOST} !^(www\.atable\.com)?$
## RewriteRule ^(.*)$ http://www.atable.com/$1 [R=301,L]


# NORMAL JIM MORGAN RULES:

# if this request is for "/" or has already been rewritten to WP
RewriteCond $1 ^(index\.php)?$ [OR]

# or if request is for image, css, or js file
RewriteCond $1 \.(gif|jpg|jpeg|png|css|js|ico)$ [NC,OR]

# or if URL resolves to existing file
RewriteCond %{REQUEST_FILENAME} -f [OR]

# or if URL resolves to existing directory
RewriteCond %{REQUEST_FILENAME} -d

# then skip the rewrite to WP
RewriteRule ^(.*)$ - [S=1]

# else rewrite the request to WP

# FINISH TO WP

### local with normal Jim Morgan's rules
RewriteRule . /www.atable-new.com/index.php [L]

### online with normal Jim Morgan's rules
## RewriteRule . /index.php [L]

### online with additional Jim Morgan's rules
## RewriteRule ^(.+)$ /index.php [L]

# END WordPress

g1smd




msg:4272612
 10:53 pm on Feb 25, 2011 (gmt 0)

The changes on the previous pages have been suggested several times over at the Wordpress developer site over the last few years. The code update has been marked as a WontFix by the people at Wordpress. You'll have to continue manually editing your installation after each upgrade in order to improve the performance.

This 41 message thread spans 2 pages: < < 41 ( 1 [2]
Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved