Mailing List Archive

Limiting redirects with rewriterule/rewritecond
Hi,

In my ongoing effort to reduce the number of redirects for
linuxsecurity.com, I could use a bit more help. Currently we have one
redirect to strip off any potential trailing slash as well as another
that strips out any preceding 'www'.

RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ $1 [R=301,L]

The rest of our redirects are of the form:

RewriteRule ^/about/us /about [L,R=301]

Should I be combining each of these to also do the above with something
like:

RewriteRule ^/about/us/? https://linuxsecurity.com/about [L,R=301]

It seems like that would reduce the number of redirects by two, but I'm
unsure of what implications that would otherwise have. Maybe if I
instead performed the RewriteConds without R=301 and just rewrote the
URL itself? I'm not sure how that works.

Any ideas greatly appreciated.
Thanks,
Dave
Re: Limiting redirects with rewriterule/rewritecond [ In reply to ]
On Sun, Feb 25, 2024 at 5:29?PM Dave Wreski
<dwreski@guardiandigital.com.invalid> wrote:

> Hi,
>
> In my ongoing effort to reduce the number of redirects for
> linuxsecurity.com, I could use a bit more help. Currently we have one
> redirect to strip off any potential trailing slash as well as another that
> strips out any preceding 'www'.
>
> RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
> RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
>
> RewriteCond %{REQUEST_FILENAME} !-d
> RewriteRule ^(.*)/$ $1 [R=301,L]
>
> The rest of our redirects are of the form:
>
> RewriteRule ^/about/us /about [L,R=301]
>
> Should I be combining each of these to also do the above with something
> like:
>
> RewriteRule ^/about/us/? https://linuxsecurity.com/about [L,R=301]
>
> It seems like that would reduce the number of redirects by two, but I'm
> unsure of what implications that would otherwise have. Maybe if I instead
> performed the RewriteConds without R=301 and just rewrote the URL itself?
> I'm not sure how that works.
>
> Any ideas greatly appreciated.
> Thanks,
> Dave
>
>
>
Perhaps you can, but be careful about not creating loops, especially if
using .htaccess files.

Also, is there a specific reason why you're not using Redirect with
mod_alias instead?
Re: Limiting redirects with rewriterule/rewritecond [ In reply to ]
> In my ongoing effort to reduce the number of redirects for
> linuxsecurity.com <http://linuxsecurity.com>, I could use a bit
> more help. Currently we have one redirect to strip off any
> potential trailing slash as well as another that strips out any
> preceding 'www'.
>
> RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
> RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
>
> RewriteCond %{REQUEST_FILENAME} !-d
> RewriteRule ^(.*)/$ $1 [R=301,L]
>
> The rest of our redirects are of the form:
>
> RewriteRule ^/about/us /about [L,R=301]
>
> Should I be combining each of these to also do the above with
> something like:
>
> RewriteRule ^/about/us/? https://linuxsecurity.com/about [L,R=301]
>
> It seems like that would reduce the number of redirects by two,
> but I'm unsure of what implications that would otherwise have.
> Maybe if I instead performed the RewriteConds without R=301 and
> just rewrote the URL itself? I'm not sure how that works.
>
> Any ideas greatly appreciated.
> Thanks,
> Dave
>
>
>
> Perhaps you can, but be careful about not creating loops, especially
> if using .htaccess files.
Do you mean because of patterns matching itself?
>
> Also, is there a specific reason why you're not using Redirect with
> mod_alias instead?

I'm not as familiar with how mod_alias works, but also thought its
functionality was more limited?

Ideas for how to do the above using mod_alias would be appreciated.

Thanks,
Dave
Re: Limiting redirects with rewriterule/rewritecond [ In reply to ]
On Thu, Feb 29, 2024 at 7:18?AM Dave Wreski
<dwreski@guardiandigital.com.invalid> wrote:

>
> In my ongoing effort to reduce the number of redirects for
>> linuxsecurity.com, I could use a bit more help. Currently we have one
>> redirect to strip off any potential trailing slash as well as another that
>> strips out any preceding 'www'.
>>
>> RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
>> RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
>>
>> RewriteCond %{REQUEST_FILENAME} !-d
>> RewriteRule ^(.*)/$ $1 [R=301,L]
>>
>> The rest of our redirects are of the form:
>>
>> RewriteRule ^/about/us /about [L,R=301]
>>
>> Should I be combining each of these to also do the above with something
>> like:
>>
>> RewriteRule ^/about/us/? https://linuxsecurity.com/about [L,R=301]
>>
>> It seems like that would reduce the number of redirects by two, but I'm
>> unsure of what implications that would otherwise have. Maybe if I instead
>> performed the RewriteConds without R=301 and just rewrote the URL itself?
>> I'm not sure how that works.
>>
>> Any ideas greatly appreciated.
>> Thanks,
>> Dave
>>
>>
>>
> Perhaps you can, but be careful about not creating loops, especially if
> using .htaccess files.
>
> Do you mean because of patterns matching itself?
>
>
> Also, is there a specific reason why you're not using Redirect with
> mod_alias instead?
>
> I'm not as familiar with how mod_alias works, but also thought its
> functionality was more limited?
>
> Ideas for how to do the above using mod_alias would be appreciated.
>
> Thanks,
> Dave
>
>
>
>
>
>
The general idea is to use separate vhosts to redirect to https://, or
enforce a canonical hostname, first.

Then, for more specific redirects, use Redirect or RedirectMatch - you can
even specify the return code (301,302,304).