Mailing List Archive

Need help understanding mod_rewrite and redirect
We have a website that has been in use for a project for years, which
someone else took up and would like the site permanently redirected to the
new URL. For whatever reason I cannot get this redirect to work
consistently, and I'd like to know where I'm going wrong.

The only thing I changed on the website.conf file was adding the redirect
statement. What used to happen was if you came in on the full base URL, it
would append the directory to the URL and load the site.

Now it alternates between showing the /var/www/index file or redirecting to
the new website, and I'm not sure why.

website.conf:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^full.website.base.URL$ [NC]
RewriteRule ^/$ https://full.website.base.URL/url-path/ [L,R]
alias /url-path "/local/www/path"
<Directory "/local/www/path">
Require all granted
RewriteEngine on
RewriteBase /url-path
Redirect permanent https://new.website.base.url/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
RewriteCond %{HTTP_HOST} !full.website.base.URL
RewriteRule ^.*$ https://full.website.base.URL%{REQUEST_URI} [R]
<Files "cron.php">
RewriteCond %{REQUEST_URI} /url-path/cron.php$ [NC]
RewriteRule "^/url-path/cron.php$"
http://full.website.base.URL%{REQUEST_URI} [L]
</Files>
</Directory>


and for good measure, I put .htaccess in the local www path:
RewriteEngine On
Redirect permanent https://new.website.base.url/
Re: Need help understanding mod_rewrite and redirect [ In reply to ]
The logs say something of the form: input=full.website.base.URL
pattern=hostname.fq.dn not matched

Where am I going wrong here?

jim

On Wed, May 3, 2023 at 9:55?AM Jim Weill <moondog@icsi.berkeley.edu> wrote:

> We have a website that has been in use for a project for years, which
> someone else took up and would like the site permanently redirected to the
> new URL. For whatever reason I cannot get this redirect to work
> consistently, and I'd like to know where I'm going wrong.
>
> The only thing I changed on the website.conf file was adding the redirect
> statement. What used to happen was if you came in on the full base URL, it
> would append the directory to the URL and load the site.
>
> Now it alternates between showing the /var/www/index file or redirecting
> to the new website, and I'm not sure why.
>
> website.conf:
> RewriteEngine On
> RewriteCond %{HTTP_HOST} ^full.website.base.URL$ [NC]
> RewriteRule ^/$ https://full.website.base.URL/url-path/ [L,R]
> alias /url-path "/local/www/path"
> <Directory "/local/www/path">
> Require all granted
> RewriteEngine on
> RewriteBase /url-path
> Redirect permanent https://new.website.base.url/
> RewriteCond %{REQUEST_FILENAME} !-f
> RewriteCond %{REQUEST_FILENAME} !-d
> RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
> RewriteCond %{HTTPS} off
> RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
> RewriteCond %{HTTP_HOST} !full.website.base.URL
> RewriteRule ^.*$ https://full.website.base.URL%{REQUEST_URI} [R]
> <Files "cron.php">
> RewriteCond %{REQUEST_URI} /url-path/cron.php$ [NC]
> RewriteRule "^/url-path/cron.php$"
> http://full.website.base.URL%{REQUEST_URI} [L]
> </Files>
> </Directory>
>
>
> and for good measure, I put .htaccess in the local www path:
> RewriteEngine On
> Redirect permanent https://new.website.base.url/
>
>
>
Re: Re: Need help understanding mod_rewrite and redirect [ In reply to ]
1) Avoid rewriting from <Directory> or <Location> blocks.
2) Avoid using mod_alias (redirect) from that context as well
3) Provide more than one rewrite log line, context matters
4) This can be replaced with FallbackResource /index.php:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

5) Why are you using mod_rewrite, either way?


On Wed, May 3, 2023 at 6:40?PM Jim Weill <moondog@icsi.berkeley.edu> wrote:

> The logs say something of the form: input=full.website.base.URL
> pattern=hostname.fq.dn not matched
>
> Where am I going wrong here?
>
> jim
>
> On Wed, May 3, 2023 at 9:55?AM Jim Weill <moondog@icsi.berkeley.edu>
> wrote:
>
>> We have a website that has been in use for a project for years, which
>> someone else took up and would like the site permanently redirected to the
>> new URL. For whatever reason I cannot get this redirect to work
>> consistently, and I'd like to know where I'm going wrong.
>>
>> The only thing I changed on the website.conf file was adding the redirect
>> statement. What used to happen was if you came in on the full base URL, it
>> would append the directory to the URL and load the site.
>>
>> Now it alternates between showing the /var/www/index file or redirecting
>> to the new website, and I'm not sure why.
>>
>> website.conf:
>> RewriteEngine On
>> RewriteCond %{HTTP_HOST} ^full.website.base.URL$ [NC]
>> RewriteRule ^/$ https://full.website.base.URL/url-path/ [L,R]
>> alias /url-path "/local/www/path"
>> <Directory "/local/www/path">
>> Require all granted
>> RewriteEngine on
>> RewriteBase /url-path
>> Redirect permanent https://new.website.base.url/
>> RewriteCond %{REQUEST_FILENAME} !-f
>> RewriteCond %{REQUEST_FILENAME} !-d
>> RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
>> RewriteCond %{HTTPS} off
>> RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
>> RewriteCond %{HTTP_HOST} !full.website.base.URL
>> RewriteRule ^.*$ https://full.website.base.URL%{REQUEST_URI} [R]
>> <Files "cron.php">
>> RewriteCond %{REQUEST_URI} /url-path/cron.php$ [NC]
>> RewriteRule "^/url-path/cron.php$"
>> http://full.website.base.URL%{REQUEST_URI} [L]
>> </Files>
>> </Directory>
>>
>>
>> and for good measure, I put .htaccess in the local www path:
>> RewriteEngine On
>> Redirect permanent https://new.website.base.url/
>>
>>
>>
Re: Re: Need help understanding mod_rewrite and redirect [ In reply to ]
On Wed, May 3, 2023 at 3:45?PM Frank Gingras <thumbs@apache.org> wrote:

> 1) Avoid rewriting from <Directory> or <Location> blocks.
>

RewriteBase says it cannot be called outside <Directory>, is that not the
case...?


> 2) Avoid using mod_alias (redirect) from that context as well
>

I tried putting the redirect outside the <Directory> and it would not
redirect at all


> 3) Provide more than one rewrite log line, context matters
>

There are several lines of that nature, but I think I've worked around this
by putting a stanza in ssl.conf to handle it


> 4) This can be replaced with FallbackResource /index.php:
>
> RewriteCond %{REQUEST_FILENAME} !-f
> RewriteCond %{REQUEST_FILENAME} !-d
> RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
>
>
I will look into this


> 5) Why are you using mod_rewrite, either way?
>

I inherited most of this many years ago. These rules were converted from
2.2 to 2.4 a few years back and they kept working until someone decided to
move the project to a new webhost outside our domain. We run several
webservers and identify the proper URL to serve based on the incoming URL,
and mod_rewrite was the easiest method at the time to make sure the correct
pages were served.
Re: Re: Need help understanding mod_rewrite and redirect [ In reply to ]
In a non-directory context, just anchor your target, and you can skip the
RewriteBase altogether.

Redirect requires two parameters in the vhost/server context.



On Wed, May 3, 2023 at 6:57?PM Jim Weill <moondog@icsi.berkeley.edu> wrote:

> On Wed, May 3, 2023 at 3:45?PM Frank Gingras <thumbs@apache.org> wrote:
>
>> 1) Avoid rewriting from <Directory> or <Location> blocks.
>>
>
> RewriteBase says it cannot be called outside <Directory>, is that not the
> case...?
>
>
>> 2) Avoid using mod_alias (redirect) from that context as well
>>
>
> I tried putting the redirect outside the <Directory> and it would not
> redirect at all
>
>
>> 3) Provide more than one rewrite log line, context matters
>>
>
> There are several lines of that nature, but I think I've worked around
> this by putting a stanza in ssl.conf to handle it
>
>
>> 4) This can be replaced with FallbackResource /index.php:
>>
>> RewriteCond %{REQUEST_FILENAME} !-f
>> RewriteCond %{REQUEST_FILENAME} !-d
>> RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
>>
>>
> I will look into this
>
>
>> 5) Why are you using mod_rewrite, either way?
>>
>
> I inherited most of this many years ago. These rules were converted from
> 2.2 to 2.4 a few years back and they kept working until someone decided to
> move the project to a new webhost outside our domain. We run several
> webservers and identify the proper URL to serve based on the incoming URL,
> and mod_rewrite was the easiest method at the time to make sure the correct
> pages were served.
>