0

I basically found a part of my solution here: Apache rewrite rule and keep the url unchanged?

but I want to do more.

I want to open a "template.php" site which comes up on any URN entered. So if I open abc.com/blabla, template opens and URL stays abc.com/blabla. If I enter abc.com/lalala, template opens and URL stays abc.com/lalala.

I think this is what I want:

RewriteRule ^/$       /template.php

Besides that, I want my mainsite abc.com to open index.php and also use a rewrite for https which I do like this:

    RewriteEngine   On
    RewriteCond     %{HTTPS} !=on
    RewriteRule     ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]

How can I achieve both in one config? Doesnt the https part mess up the URN part?

  • "I think this is what I want" - And what does that do? What do you think `^/$` means? "any URN" - do you literally mean _any_ URL, even URLs that would otherwise map directly to a filesystem resource? Just to confirm... these directives are in your main server config / virtual host container? – MrWhite Apr 30 '18 at 16:03
  • abc.com/ should point to template.php, the only exception is abc.com/index.php which is the mainsite and should point to index.php (+ https). Yes, template.php and index.php are located in my rootdir /var/www// – CodingDude May 02 '18 at 12:45

1 Answers1

0
RewriteRule ^/$       /template.php

Assuming this directive is in a server or virtualhost context (ie. not a directory or .htaccess context), then this will only match requests for the document root (ie. /) and ignore /blabla and /lalala.

To match <anything>, except index.php (or an empty URL-path, assuming index.php is set as the DirectoryIndex) then do something like the following instead:

 RewriteRule ^/(index\.php|template\.php)?$ - [L]
 RewriteRule ^ /template.php [L]

The first rule stops any further processing if / or /index.php or /template.php is requested, which allows your main site (ie. index.php) to be accessed and prevents a rewrite loop.

The second rule rewrites all other URLs (whether they map directly to filesystem resources or not) to /template.php.

It's unusual that you wouldn't want static resources (images, CSS, JS, etc.) to be excluded from this rewrite, but you've confirmed this in comments.

RewriteEngine   On
RewriteCond     %{HTTPS} !=on
RewriteRule     ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]

Since you are in the main server config (ie. not .htaccess) then you shouldn't be using mod_rewrite to issue the HTTP to HTTPS redirect anyway. You should have a simple mod_alias Redirect in the non-canonical <VirtualHost *:80> container.

Doesnt the https part mess up the URN part?

No. (To answer this specific question - if you were using mod_rewrite.)

This is of course assuming you have the directives in the correct order. The external redirect from HTTP to HTTPS should appear first.

MrWhite
  • 12,647
  • 4
  • 29
  • 41