homepage Welcome to WebmasterWorld Guest from 23.23.12.202
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
.htaccess Meltdown
Looking at You With Sad Puppy Eyes
SevenCubed

WebmasterWorld Senior Member



 
Msg#: 4603044 posted 6:32 pm on Aug 18, 2013 (gmt 0)

Hi folks,

I know the philosophy of this forum quite well. I know it's not intended to be a .htaccess writing service.

However, I've been having one technical meltdown after another for weeks now and I'm feeling empty and numb.

If I have to be put through the "lecture ringer" I'm sure I can bare with it but please do be gentle...<looking at you with sad puppy eyes>.

It's simple enough but not working. I am used to working directly in apache config files (httpd.conf, vhosts.conf) but this one is related to .htaccess so it might be a dot or slash in wrong place or not there when it should be or there when it shouldn't. I dunno anymore.

I just need to redirect a Wordpress page that's linked as:

www.example.com/?page_id=200 (which is actually named "actual-page-name-old" in Wordpress panel but pointed to "http://www.example.com/?page_id=200" on most public links).
to
www.example.com/actual-page-name/

This is further complicate (for me) because of a total lack of experience with Wordpress. From what I've been able to understand wordpress doesn't actually haves "page" but rather "stubs" that are created to return a virtual page that is identified and pointed to be the "?page_id=200"?

I've tried many variations throughout the day and nothing is working :(

Edit: sorry didn't mean to drop the link reference in one of the URL's to actual site, caught it after posting, changed to example.com

 

SevenCubed

WebmasterWorld Senior Member



 
Msg#: 4603044 posted 7:50 pm on Aug 18, 2013 (gmt 0)

I guess I should at least have added some examples I've tried:

RewriteEngine On
RewriteRule ^?page_id=200$1 http://www.example.com/page-name/$1 [R=301,L] <-- this one produced real nasty results
RewriteRule ^/page-name-old/$1 http://www.example.com/page-name/$1 [R=301,L]
RewriteRule http://www.example.com/page-name-old/$1 http://www.example.com/page-name/$1 [R=301,L]
RewriteRule http://www.example.com/?page_id=200 http://www.example.com/page-name/$1 [R=301,L]

And, no, I didn't have them all in at the same time, tried them one after another.

I inherited this site from a previous developer/maintainer and was very reluctant about it because the internal page structure is messy plus me not being familiar with Wordpress. When I met with these people I certainly expressed those concerns and offered to revamp it into a better streamlined non-Wordpress version but they didn't want to. I only took it on because it's the husband's company of one of my existing clients that referred them to me. Not that this has anything to do with my actual OP problem, it's just background fluff to help you understand how I've arrived at this predicament.

aakk9999

WebmasterWorld Administrator 5+ Year Member



 
Msg#: 4603044 posted 9:48 pm on Aug 18, 2013 (gmt 0)

I am not an expert at all, but perhaps you can try the following:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /\?page_id=200\ HTTP/
RewriteRule ^$ http://www.example.com/actual-page-name/? [R=301,L]

I think you need to put this in your .htaccess before the Wordpress section where all requests are passed onto index.php

My understanding (and I stand to be corrected) is that on the left hand side of RewriteRule you cannot have query string. So you first put RewriteCond to capture this particular request, i.e. so that the rule is only executed if the request is /?page_id=200

Then the left side of RewriteRule which should be the pattern after the domain name but without the query string ends up being empty, hence ^$
The right side is the fully qualified target URL including protocol and domain name and the ? at the end of it strips off the query string.

SevenCubed

WebmasterWorld Senior Member



 
Msg#: 4603044 posted 10:56 pm on Aug 18, 2013 (gmt 0)

EUREKA ACES !

Oh my gosh aakk9999 you are a life saver here. It worked!

I am seriously grateful for your reply. It worked as you posted it. I just added [NC] at the end of the first line just in case.

I still have another dilemma that's related and tied in with my OP too but with this foundation you've provided I'll be able to find my way out of the darkness from here.

Also congrats (condolences? (kidding)) on your promotion to moderator. I saw a thread concerning that the other day but was so buried in situations here that I didn't even take time to login to say so.

phranque

WebmasterWorld Administrator phranque us a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



 
Msg#: 4603044 posted 11:41 pm on Aug 18, 2013 (gmt 0)

RewriteRule http://www.example.com/?page_id=200 http://www.example.com/page-name/$1 [R=301,L]


http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewriterule
On the first RewriteRule, (the Pattern) is matched against the (%-decoded) URL-path (or file-path, depending on the context) of the request. Subsequent patterns are matched against the output of the last matching RewriteRule.


this means you will never match your pattern with the hostname or the query string.
if you have that requirement you can use a RewriteCond directive and match against the HTTP_HOST or QUERY_STRING server variables.


RewriteRule ^/page-name-old/$1 http://www.example.com/page-name/$1 [R=301,L]

your use of the $1 in the Pattern is incorrect syntax.
the $1 is intended to be used as a back-reference variable in the Substitution string.
assuming your intention was to end-anchor the Pattern, that's just a dollar sign ($) without the digit.

lucy24

WebmasterWorld Senior Member lucy24 us a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



 
Msg#: 4603044 posted 12:27 am on Aug 19, 2013 (gmt 0)

Well, who could be unkind to sad puppy-dog eyes? :)

I just added [NC] at the end of the first line just in case.

The [NC] flag is fine for redirects. But make sure you don't use it in rewrites, unless you're rewriting to a php-type page that will make its own decisions about correct casing.

WordPress seems to be based on a lot of stuff happening magically behind the scenes, so it can be used by people who know absolutely nothing. This is fine up until the moment you need to do something non-standard.

phranque

WebmasterWorld Administrator phranque us a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



 
Msg#: 4603044 posted 12:34 am on Aug 19, 2013 (gmt 0)

RewriteRule ^/page-name-old/$1 http://www.example.com/page-name/$1 [R=301,L]


i forgot to mention one thing.

http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewriterule

- When using the rewrite engine in .htaccess files the per-directory prefix (which always is the same for a specific directory) is automatically removed for the RewriteRule pattern matching and automatically added after any relative (not starting with a slash or protocol name) substitution encounters the end of a rule set. See the RewriteBase directive for more information regarding what prefix will be added back to relative substitutions.
- ...
- The removed prefix always ends with a slash, meaning the matching occurs against a string which never has a leading slash. Therefore, a Pattern with ^/ never matches in per-directory context.


if you were implementing this RewriteRule in the server config context it would be appropriate to match against the leading slash.

not2easy

WebmasterWorld Administrator 5+ Year Member Top Contributors Of The Month



 
Msg#: 4603044 posted 5:33 am on Aug 19, 2013 (gmt 0)

WordPress seems to be based on a lot of stuff happening magically behind the scenes, so it can be used by people who know absolutely nothing. This is fine up until the moment you need to do something non-standard.


Very true. Unless there is some setting that prevents it (either in the general settings or the selected permalink structure or some plugin), you can usually just click to edit a page or post, then edit the title you want to change in WP and it automatically rewrites to the new name. I caught a horribly mangled spelling of a word used in a page's title (and so its URL) and just changing the spelling created the new URL and rewrote requests to the new name.

(edited to fix punctuation)

g1smd

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4603044 posted 9:43 am on Aug 19, 2013 (gmt 0)

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /\?page_id=200\ HTTP/
RewriteRule ^$ http://www.example.com/actual-page-name/? [R=301,L]


I would make two small changes to ak9999 code:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(index\.php)?\?page_id=200\ HTTP/
RewriteRule ^(index\.php)?$ http://www.example.com/actual-page-name/? [R=301,L]


This allows requests for example.com/?page_id=200 and for example.com/index.php?page_id=200 to be redirected.

Be aware of another limitation. If there are additional parameters requested, the request will not be redirected.

Make sure this redirect is placed before your site-wide non-www to www redirect. This avoids an unwanted redirection chain for non-www requests.

aakk9999

WebmasterWorld Administrator 5+ Year Member



 
Msg#: 4603044 posted 12:12 pm on Aug 19, 2013 (gmt 0)

Thanks on improving my basics!

I looked it up and (index\.php)? means "or optionally if also exists index.php in the matching partern"

Be aware of another limitation. If there are additional parameters requested, the request will not be redirected.

Would adding an additional RewriteCond work for an additional parameter in URL? E.g.

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(index\.php)?\?page_id=200\ HTTP/ [OR]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(index\.php)?\?page_id=200&
RewriteRule ^(index\.php)?$ http://www.example.com/actual-page-name/? [R=301,L]


Of course, only if the additional parameter does not change the page content and is either a presentational or a tracking parameter.

Panthro



 
Msg#: 4603044 posted 6:56 pm on Aug 19, 2013 (gmt 0)

So you guys are the ones hacking all the WP sites!





lol

lucy24

WebmasterWorld Senior Member lucy24 us a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



 
Msg#: 4603044 posted 8:22 pm on Aug 19, 2013 (gmt 0)

Would adding an additional RewriteCond work for an additional parameter in URL?
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(index\.php)?\?page_id=200\ HTTP/ [OR]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(index\.php)?\?page_id=200&

You could collapse them into a single Condition:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(index\.php)?\?page_id=200(?:&\S+)?\ HTTP/

If your server is cranky about \S, replace with [^\ ]. (In htaccess, literal spaces have to be escaped-- even inside grouping brackets-- because they have syntactic meaning. In ordinary PCRE they don't.)

g1smd

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4603044 posted 11:00 pm on Aug 19, 2013 (gmt 0)

So you guys are the ones hacking all the WP sites!

After lengthy discussion among a number of people here, I eventually took a set of suggested htaccess optimisations over to Wordpress but they weren't interested.

lucy24

WebmasterWorld Senior Member lucy24 us a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



 
Msg#: 4603044 posted 2:26 am on Aug 20, 2013 (gmt 0)

How odd. Admittedly I've only once had an extended interaction with someone at WP-- but they certainly seemed to have working brain cells! :(

g1, the trick is to go to the last page of your StickyMail inbox, Select All ... and delete that sucker without looking. It's the only thing I've found to work.

aakk9999

WebmasterWorld Administrator 5+ Year Member



 
Msg#: 4603044 posted 12:07 pm on Aug 20, 2013 (gmt 0)

You could collapse them into a single Condition:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(index\.php)?\?page_id=200(?:&\S+)?\ HTTP/


Thanks Lucy, learning something new every day :)

SevenCubed

WebmasterWorld Senior Member



 
Msg#: 4603044 posted 8:23 pm on Aug 29, 2013 (gmt 0)

Many thanks to all of you here for your help.

@aakk9999 for the solution, @g1smd for the fine tuning suggestions -- I implemented them, @phranque for educational encouragement (I'll get around to it someday, really I will!), @not2easy for confirming gremlins in the details of WP, @Panthro for injecting some humour when I needed it most, and @lucy24 -- just because -- how could we live without you?!

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