logo
Apache Lounge
Webmasters

 

About Forum Index Downloads Search Register Log in RSS X


Keep Server Online

If you find the Apache Lounge, the downloads and overall help useful, please express your satisfaction with a donation.

or

Bitcoin

A donation makes a contribution towards the costs, the time and effort that's going in this site and building.

Thank You! Steffen

Your donations will help to keep this site alive and well, and continuing building binaries. Apache Lounge is not sponsored.
Post new topic   Forum Index -> Apache View previous topic :: View next topic
Reply to topic   Topic: Newbie question about redirects
Author
shortmort37



Joined: 26 Sep 2024
Posts: 6
Location: Philadelphia, USA

PostPosted: Thu 26 Sep '24 21:48    Post subject: Newbie question about redirects Reply with quote

Linux: 4.18.0 x86_64
Apache: 2.4.62
PHP: 7.4.33

I've been studying the documentation on mod_rewrite, but I have to confess most of it reads like stereo instructions to me. But I can put into words what I want to do.

My server will be presented with a URL of this form:

PREFIX59plymouth.netSUFFIX

Here's what I'd like to do:

1) If PREFIX is null, prepend https:// to the URL
2) If PREFIX is not null, replace it with https:// (http[s]:// could be present, or absent; same for www.)
3) If SUFFIX is null, append /59forum
4) If SUFFIX is not null, append SUFFIX (which will contain /59forum, but could contain more)

Could someone point me in the right direction?

Thanks in advance,
Dan
Back to top
shortmort37



Joined: 26 Sep 2024
Posts: 6
Location: Philadelphia, USA

PostPosted: Mon 30 Sep '24 23:27    Post subject: Reply with quote

*BUMP*.

Everything is working, except for the SUFFIX part; if present, I'd like for it to carry over. Whatever its value is - essentially, to wildcard it.

Any help?
Back to top
tangent
Moderator


Joined: 16 Aug 2020
Posts: 346
Location: UK

PostPosted: Tue 01 Oct '24 16:35    Post subject: Reply with quote

You've not posted your current rewrite rule logic, which would help.

Your suffix requirement 4) is a bit ambiguous. Do you mean 'prepend' /59forum to the request uri if it's missing? i.e. if the request is for /page.html should it be rewritten to /59forum/page.html

If so, try something like the following rewrite rules.
Code:
RewriteRule ^/$ /59forum [L,QSA,R]

RewriteCond %{REQUEST_URI} !^/59forum.*
RewriteRule ^/(.+)$ /59forum/$1 [L,QSA,R]

The first one copes with empty site root redirects, whilst the second prefixes /59forum to any existing uri if it's not already present. Note the various rewrite flags, where QSA means append any query string present. You might want to add an NE flag as well (see https://httpd.apache.org/docs/current/rewrite/flags.html)

To help work out what's going on with mod_rewrite processing, you can increase the loglevel for that module.
Code:
LogLevel rewrite:trace8
Back to top
shortmort37



Joined: 26 Sep 2024
Posts: 6
Location: Philadelphia, USA

PostPosted: Tue 01 Oct '24 16:51    Post subject: Re: Newbie question about redirects Reply with quote

Apologies - here are my existing rules:

Code:
RewriteEngine on

RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

RewriteCond %{HTTP_HOST} ^59sportfury\.net$ [OR]
RewriteCond %{HTTP_HOST} ^www\.59sportfury\.net$
RewriteRule ^/?$ "https\:\/\/59plymouth\.net\/59forum" [R=301,L]

RewriteCond %{HTTP_HOST} ^59plymouth\.net$ [OR]
RewriteCond %{HTTP_HOST} ^www\.59plymouth\.net$
RewriteRule ^/?$ "https\:\/\/59plymouth\.net\/59forum" [R=301,L]


These all work fine - http is redirected to https, www is stripped, 59sportfury is mapped to 59plymouth, and /59forum is appended (the location of my phpBB board). And any combinations thereof.

The rub comes when there's a post on my board that contains a link that redirects to another post, e.g.:

http://www.59sportfury.net/59forum/viewtopic.php?t=1234

I want for all of the rules above to apply, but for /59forum/* to be appended instead, where "*" wildcards whatever follows /59forum.

The LogLevel directive - is that for httpd.conf, or can it also go in .htaccess (while I'm testing)?

Thanks
Dan
Back to top
tangent
Moderator


Joined: 16 Aug 2020
Posts: 346
Location: UK

PostPosted: Tue 01 Oct '24 22:31    Post subject: Reply with quote

I think your problem stems from your current redirect rules which only pick up site root requests ^/?$ - this won't match your cross site posts.

Believe the following rules should cover what you're trying to achieve:

Code:
RewriteRule ^/$ /59forum/ [L,R]

RewriteCond %{HTTP_HOST} ^59plymouth.net$ [OR]
RewriteCond %{HTTP_HOST} ^www.59plymouth.net$ [OR]
RewriteCond %{HTTP_HOST} ^59sportfury.net$ [OR]
RewriteCond %{HTTP_HOST} ^www.59sportfury.net$
RewriteRule ^/(.*)$ https://59plymouth.net/$1 [R=302,L,QSA]

However, if all the host variants are really the same physical host, and assuming your using name-based virtual hosts in your httpd.conf file, could you not just add a ServerAlias entry after the Servername, to pick up all the other host references, e.g.

Code:
ServerName 59plymouth.net
ServerAlias www.59plymouth.net 59sportsfury.net www.59sportsfury.net

In which case you won't need the redirect rewrite rules.
Back to top
shortmort37



Joined: 26 Sep 2024
Posts: 6
Location: Philadelphia, USA

PostPosted: Tue 01 Oct '24 23:35    Post subject: Reply with quote

tangent wrote:

However, if all the host variants are really the same physical host, and assuming your using name-based virtual hosts in your httpd.conf file, could you not just add a ServerAlias entry after the Servername, to pick up all the other host references, e.g.

Code:
ServerName 59plymouth.net
ServerAlias www.59plymouth.net 59sportsfury.net www.59sportsfury.net

In which case you won't need the redirect rewrite rules.


I'll take those changes out for a spin. Thanks!

I do have access to the httpd.conf file, where 59sportfury.net is already defined as an alias to 59plymouth.net. The issue is that while the root directory is found, the name is not substituted - and I am warned that the address needs to be whitelisted (with OneAll, a third party integration system used with Facebook). I'd rather just constrain addresses to a common format.

I'll get back with the outcome. Thanks again.

Dan
Back to top
shortmort37



Joined: 26 Sep 2024
Posts: 6
Location: Philadelphia, USA

PostPosted: Wed 02 Oct '24 0:14    Post subject: Reply with quote

I left out the https rule, and included just your suggestions:

Code:
RewriteRule ^/$ /59forum/ [L,R]

RewriteCond %{HTTP_HOST} ^59plymouth.net$ [OR]
RewriteCond %{HTTP_HOST} ^www.59plymouth.net$ [OR]
RewriteCond %{HTTP_HOST} ^59sportfury.net$ [OR]
RewriteCond %{HTTP_HOST} ^www.59sportfury.net$
RewriteRule ^/(.*)$ https://59plymouth.net/$1 [R=302,L,QSA]


Here's what I get:
Code:
https://www.59sportfury.net
is not rewritten to include
Code:
/59forum
; nor is
Code:
https://www.59plymouth.net
- nor either, without a
Code:
www.
prefix. None of those are changed, while they were before, to include
Code:
/59forum
. Nor is anything rewritten, if those four formulations include
Code:
/59forum
.

If a post link includes, e.g.,
Code:
www.59plymouth.net/59forum/viewtopic.php$t=1234
, the browser is directed to that post and
Code:
www.
is removed. However,
Code:
59sportfury.net
does not follow suit - the hostname is not changed.

Let me restate what I'm trying to accomplish:

Code:
PREFIXhostnameSUFFIX


If prefix is http://, substitute https://. This alread works with this rule:

RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

If hostname contains
Code:
www.
, remove it; if it contains
Code:
59sportfury.net
, substitute
Code:
59plymouth.net
.

If SUFFIX is NULL, append /59forum; if SUFFIX is NOT NULL, append it instead.

All of this is working, except for this:

Quote:
...if SUFFIX is NOT NULL, append it instead.


Dan
Back to top
tangent
Moderator


Joined: 16 Aug 2020
Posts: 346
Location: UK

PostPosted: Wed 02 Oct '24 21:06    Post subject: Reply with quote

The rewrite rules I posted were based on a test instance of Apache added to a copy of the default httpd.conf, along with a VirtualHost section, plus local host file entries for your various site URLs. I've not used .htaccess files, which will be directory specific, and potentially cloud issues.

What the trace log messages show me is how the request uri gets rewritten in transit through the rules, and it looks like I missed your "...if SUFFIX is NOT NULL, append it instead" use case.

So believe you need to use several rewrite sections.
    1) One for your http to https redirect, which you already have

    2) One to rewrite a site root request to /59forum/

    3) One to redirect requests with the incorrect HTTP_HOST to 59plymouth.net

    4) One to handle your "...if SUFFIX is NOT NULL" use case
However, I'm still somewhat confused as to what you mean by "...if SUFFIX is NOT NULL, append it instead". To me append means add /59forum at the end of the current uri, but I don't think that's what you mean.

Do you mean "...if SUFFIX is NOT NULL, and it doesn't start with /59forum, then prepend /59forum to the SUFFIX"?

Would a specific example be:- https://59plymouth.net/abc => https://59plymouth.net/59forum/abc

If so I believe this rewrite rule from my initial reply above would cover section 4).
Code:
RewriteCond %{REQUEST_URI} !^/59forum.*
RewriteRule ^/(.+)$ /59forum/$1 [L,QSA,R]

Using the following rules, for 2), 3) and 4), my test instance seems to cover the various scenarios you've listed:
Code:
RewriteRule ^/$ /59forum/ [R,L]

RewriteCond %{HTTP_HOST} ^www.59plymouth.net$ [OR]
RewriteCond %{HTTP_HOST} ^59sportfury.net$ [OR]
RewriteCond %{HTTP_HOST} ^www.59sportfury.net$
RewriteRule ^/(.*)$ https://59plymouth.net/$1 [R=302,L,QSA]   

RewriteCond %{REQUEST_URI} !^/59forum.*
RewriteRule ^/(.+)$ /59forum/$1 [L,QSA,R]

Hope this helps; am not sure what else to advise.

PS - you've previously specified 301 redirects, which are considered permanant, and will be cached by browsers. I favour 302 redirects, which are temporary and allow for server side changes.
Back to top
shortmort37



Joined: 26 Sep 2024
Posts: 6
Location: Philadelphia, USA

PostPosted: Wed 02 Oct '24 22:39    Post subject: Reply with quote

tangent wrote:
One to handle your "...if SUFFIX is NOT NULL" use case[/list]However, I'm still somewhat confused as to what you mean by "...if SUFFIX is NOT NULL, append it instead". To me append means add /59forum at the end of the current uri, but I don't think that's what you mean.

Do you mean "...if SUFFIX is NOT NULL, and it doesn't start with /59forum, then prepend /59forum to the SUFFIX"?

Would a specific example be:- https://59plymouth.net/abc => https://59plymouth.net/59forum/abc


Here are two very specific examples, where SUFFIX is present, or not:

Code:
http://www.59sportfury.net/59forum/viewtopic.php?t=1234


I want that rendered as:

Code:
https://59plymouth.net/59forum/viewtopic.php?t=1234


In this case, SUFFIX is NOT NULL - I want it appended to the rewrite. Then, there is the case that is already being handled, where SUFFIX is NULL:

Code:
http://www.59sportfury.net


I want this rendered as:

Code:
https://59plymouth.net/59forum


Thank you for all of your efforts! I will check it out.

Dan
Back to top


Reply to topic   Topic: Newbie question about redirects View previous topic :: View next topic
Post new topic   Forum Index -> Apache