Forum Moderators: phranque

Message Too Old, No Replies

small wp seo rewriteRule to make a redirect

         

Grimmjow

6:21 pm on Mar 4, 2009 (gmt 0)

10+ Year Member



I made a static page in wp reachable through /mypage/

I want to redirect all URLs in the form:

/mypage/bla/bla.htm

to

/mypage/?q=bla/bla.htm

to process $_GET['q'] from the page.

So I tried:

<IfModule mod_rewrite.c>
RewriteEngine On

#my try
RewriteCond %{REQUEST_URI} ^/mypage/([^\?\=]+)$
RewriteRule ^mypage/([^\?\=]+)$ mypage/?q=$1 [L]

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule . - [L]
RewriteRule ^([_0-9a-zA-Z-]+/)?(wp-.*) $2 [L]
RewriteRule ^([_0-9a-zA-Z-]+/)?(.*\.php)$ $2 [L]
RewriteRule . index.php [L]
</IfModule>

The outcome is not any internal server error, but 404, because it's loaded the default wordpress "page not found" page with a search box.

I had a look to a similar thread to come up with ideas:
[webmasterworld.com...]

Thanks in advance for any help.

[edited by: Grimmjow at 6:25 pm (utc) on Mar. 4, 2009]

jdMorgan

8:02 pm on Mar 4, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'd suggest:

RewriteEngine on
#
# Rewrite mypage/<something>/<otherthing>.htm to /mypage/?q=<something>/<otherthing>.htm
RewriteRule ^mypage/([^/]+/[^.]+\.htm)$ mypage/?q=$1 [L]
#
# Skip all subsequent rules if requested URL resolves to an existing
# file or directory, or if the request is for "mypage/<anything>"
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_URI} ^/mypage/
RewriteRule . - [L]
#
RewriteRule ^([_0-9a-zA-Z-]+/)?(wp-.+) $2 [L]
RewriteRule ^([_0-9a-zA-Z-]+/)?(.+\.php)$ $2 [L]
RewriteRule . index.php [L]

The <IfModule> container is only needed if you plan to deploy this code on multiple servers, and you want it to fail silently if mod_rewrite is not installed.

Note that you could easily drop the ".htm" from the "q=" value before calling the script, simply by moving the closing parenthese ahead of the "\." in the RewriteRule pattern.

Jim

Grimmjow

8:32 pm on Mar 4, 2009 (gmt 0)

10+ Year Member



First of all thanks for helping.

It looks clear and correct to me, but WP still loads the "page not found" page.

Maybe the glitch is somewhere else :(

jdMorgan

3:30 pm on Mar 5, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



With the code above, and assuming that "/mypage/" refers to a physically-existing directory with an index.php file (or similar) defined as its DirectoryIndex, this should work. The other possibilities are:

1) Transcription errors, such as changing more than just the name of "mypage" and accidentally deleting or inserting an extra slash or other character.
2) Other code interfering with this code -- either in a directory above this .htaccess file's directory, or in the server config itself.
3) mod_alias, MultiViews (mod_negotiation), or AcceptPathInfo (Apache 2.x only) grabbing the request before mod_rewrite can modify it.

Let me know if my assumptions about the meaning of "mypage" were incorrect.

Jim