Forum Moderators: phranque

Message Too Old, No Replies

Struggling with a RewriteRule

I need help getting this working

         

stoveboltgeek

5:58 pm on Jul 6, 2022 (gmt 0)

Top Contributors Of The Month



I manage, on a volunteer basis, a busy hobby website with about 5600 pages, both static and dynamic content. About 860 pages have links on them to a bulletin board that we used to use before it was changed by the vendor (utlimatebb - changed to ubbthreads).

I'm trying to figure out how to redirect those links to the new bulletin board so that they will no longer generate 404 errors.

Here's the old link.
https://www.example.com/bboard/cgi-bin//ultimatebb.cgi?ubb=get_profile;u=00009498

And here's the new link.
https://www.example.com/ubbthreads/ubbthreads.php?ubb=showprofile&User=00009498

I've been doing a ton of reading trying to figure this out. If I understand the docs correctly, I have to have a RewriteCond that looks for the query string, the a RewriteCond that looks for the REQUEST_URI, and then put the two together for the RewriteRule.

I'm really unsure how to do this, but this is what I wrote. (It doesn't work.)
RewriteCond “%{REQUEST_URI}” “/bboard/cgi-bin/ultimatebb.cgi”
RewriteCond “%{QUERY_STRING}” “ubb=showprofile&User=(\d*)”
RewriteRule ^/bboard.*.cgi$ /ubbthreads/ubbthreads.php?ubb=showprofile&User=%1”

Have I completely misunderstood how this works? Or did I just write the conditions or rules incorrectly?

[edited by: phranque at 9:18 pm (utc) on Jul 6, 2022]
[edit reason] Please Use example.com For Domain Names in Posts Apache Web Server forum [/edit]

phranque

9:41 pm on Jul 8, 2022 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



For clarification, I should change this .htaccess rule.
RewriteRule ^links/ /links.html [L,R=301]

to this if I move it to the conf file?
RewriteRule /links/ /links.html [L,R=301]

i would suggest this in the server config file:
RewriteRule ^/links/ https://www.example.com/links.html [L,R=301]


Are conf rules always referenced from DocumentRoot?

please reread the "What is matched?" section of the Apache doc i quoted above.
if the "conf rules" (specifically the RewriteRule directives) are in VirtualHost context (i.e. not in a <Directory> envelope) the Pattern will be matched with:
the part of the URL after the hostname and port, and before the query string

if the RewriteRule directives are in .htaccess or in a <Directory> envelope the Pattern will be matched with:
the portion of the currently mapped filesystem path "below" where the rule is defined

(hence why the leading slash is stripped from the path before matching in .htaccess context at the document root level)

robzilla

10:40 pm on Jul 8, 2022 (gmt 0)

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



RewriteRule /links/ /links.html [L,R=301]

You could, but note that, since it's a regular expression, "/links/" will match not only /links/ but also a path like /whatever/links/.

For a straightforward 1-to-1 redirect you could also do:
Redirect 301 /links/ https://www.example.com/links.html

(But that won't redirect any other pages under /links/ besides the index page)

I confess that Apache docs are often too technical for me to understand, but I try my best.

Not just for you, it's quite complex, I don't like having to deal with them myself. Thankfully I'm an nginx user these days, which I find a little easier.

phranque

11:11 pm on Jul 8, 2022 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



For a straightforward 1-to-1 redirect you could also do:

Redirect 301 ...

you don't want to mix mod_alias and mod_rewrite directives in the same configuration.

lucy24

11:45 pm on Jul 8, 2022 (gmt 0)

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



Apache docs persist in saying that redirects are best done with mod_alias, and you should ONLY use mod_rewrite IF ...
 
and by the time you've assimilated all the IFs, you’ve pretty well covered every website in the world. Thanks, Apache.

phranque

1:25 am on Jul 9, 2022 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



you should never use the mod_alias directives (Redirect/RedirectMatch) when you are using mod_rewrite.
use an equivalent RewriteRule instead.

When not to use mod_rewrite:
https://httpd.apache.org/docs/current/rewrite/avoid.html [httpd.apache.org]
when there are Redirect and RewriteRule directives in the same scope, the RewriteRule directives will run first, regardless of the order of appearance in the configuration file

stoveboltgeek

2:21 am on Jul 9, 2022 (gmt 0)

Top Contributors Of The Month



I'm still reading - and learning. Thanks for the in-depth discussion.
This 36 message thread spans 2 pages: 36