Forum Moderators: phranque

Message Too Old, No Replies

Need help with .htaccess and Redirect

         

mrchocobo

9:51 am on Oct 10, 2008 (gmt 0)

10+ Year Member



I have a blog and a wiki at my website.

But I'm a little tired of having to delete a lot of spam from the wiki, so I was thinking about removing it and redirecting that traffic to newly created pages at my blog.

I'm using dokuwiki for the wiki, so the urls look like this:

mywebsite.com/wiki/doku.php
mywebsite.com/wiki/doku.php?id=this_is_a_title
mywebsite.com/wiki/doku.php?id=just_another_one

and I would like those URLs to redirect with a 302 to
mywebsite.com/wiki/
mywebsite.com/this-is-a-title/
mywebsite.com/just-another-one/

Does anyone know how can I do this with htaccess and rewriterule?

Thank you very much.


By the way, I don't have any problems with having to write one rule for each URL, because I don't have too many pages at the wiki.

g1smd

11:40 am on Oct 10, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



If the page names are always an exact match, then you will need only a few general rules to do this.

Why do you want a 302 redirect? What is your thought there? Why not 301?

Which URL, old or new, do you want visitors to see in their browser?

Which URL, old or new, do you want indexed by search engines?

The code is fairly easy. The biggest part of the job is defining exactly what you want it to do.

What you want to do is asked every day in this forum. There is plenty of example code to play with.

After research in the sticky threads at the top of the forum, and other recent posts, post your best effort code here as a basis for discussion.

mrchocobo

12:46 pm on Oct 10, 2008 (gmt 0)

10+ Year Member



Sorry, I meant a 301 code, of course.

I'm not any good at regular expressions but I'll try.

Would something like this work?

RewriteRule ^wiki/doku.php\?id=(.*)$ http://example.com/%1/ [R=301,L]

But the URL could have more params like the session id. Do I need to use %{QUERY_STRING}?

[edited by: jdMorgan at 2:07 pm (utc) on Oct. 10, 2008]
[edit reason] Please use example.com [/edit]

jdMorgan

2:12 pm on Oct 10, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If the sessionID is present, does it always lead or follow your "id=" parameter, or could the two name/value pairs be in either order? The solution is far simpler and easier to understand if they're in fixed order...

Try to define everything as tightly as possible: What valid characters could be in an "id=" value? What valid characters are used in your sessionIDs? What does a requested URL look like if the sessionID is present?

As g1smd said, spend your time defining the problem well, and the coding becomes trivial. If the problem is not defined well, then coding is a waste of time -- The code may be valid and work, but not solve the problem it was intended to solve.

Jim

g1smd

3:20 pm on Oct 10, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Yes, you will also need to look at %{QUERY_STRING} at some point.

This other recent thread tackles a similar problem, but you will need to fully understand every line of code before loading any of it on to your server. Wrongly implemented code has the power to stop your website from working, and delist your entire site from search engines:
[webmasterworld.com...]

mrchocobo

5:45 pm on Oct 10, 2008 (gmt 0)

10+ Year Member



Ok. So what about this? will this work?

RewriteCond %{QUERY_STRING} (id=)
RewriteRule ^wiki/doku.php$ http://example.com/wiki/ [R=301,L]

RewriteCond %{QUERY_STRING} (id=.*)
RewriteRule .* /%1 [R=301,L]

If the user didn't specified the id of some article, they want the index of the wiki, so I redirect to http://example.com/wiki/ where I put an index.html saying that the wiki no longer exists, but they can read it at URLs foo, bar, ...

If the user selected an article (i.e. id has some value "foo") it should redirect to example.com/foo.

g1smd

6:13 pm on Oct 10, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You'll need
&?id=([^&]+)&?
to pick off the ID itself, from the query string, on the first line of the first rule and then store it in the %1 server variable.

Also on that first rule, add

?
to the end of the URL on the right of the second line, to clear the query string in the redirect. You'll also transfer the ID in the %1 server variable on to the target URL. Don't forget to escape any literal periods on the "left" too.

That rule now properly redirects if there is an ID present.

RewriteCond %{QUERY_STRING} [b]&?id=([^&]+)&?[/b]
RewriteRule ^wiki/dok[b]u\[/b].php$ http://example.com/wiki/[b]%1?[/b] [R=301,L]

Forget the second rule for a moment until you have the first one working.

I believe the second rule can be just:

 RewriteRule ^wiki/doku.php$ http://example.com/wiki/ [R=301,L] 

because the parser will only get this far down the list of redirects if the one above has not already redirected the user away. This will redirect if there is no query string, or if the query string contains junk.

You will also need the standard "strip the index name" and "redirect non-www to www" code here as well.

.

This other thread [webmasterworld.com...] may also help with some ideas, but is for a more complex example with two parameters (and a final rewrite that you don't need here).