1

I am moving a blog hosted on an Apache server to a new domain.

The permalinks are kept the same for the blog posts but there are a few pages where the url slug will change on the new domain.

My question is if the following is possible and how I would do that with rewrite rules.

The URL slugs/permalinks of the blog posts and most of the pages on the old domain will stay the same on the new domain. So I imagine that I could add a redirect rule that redirects from https://huiskopenomteverhuren.nl/ to https://vastgoedmentor.com as it will find the same /slug on the new domain

Some pages of the old website have moved to new permalink. So I need additional rules to redirect for example https://huiskopenomteverhuren.nl/kennisbank/ to https://vastgoedmentor.com/resources and do this for a few other pages as well.

1 Answers1

0

You can create multiple RewriteRule directives with regular expressions within your server configuration. You should start with the specific redirects with changed permalinks. The general redirect for all other pages can be added at the end.

RewriteEngine On

RewriteRule ^/kennisbank(/.*)?$ https://vastgoedmentor.com/resources$1 [END,R=301]
RewriteRule ^/old2(/.*)?$       https://vastgoedmentor.com/new2$1      [END,R=301]
RewriteRule ^/old3(/.*)?$       https://vastgoedmentor.com/new3$1      [END,R=301]

RewriteRule ^/(.*)$           https://vastgoedmentor.com/$1           [END,R=301]

The notation of (/.*)? will perform the redirect with and without a trailing slash and add any additional path details to the new URL. Hence, it will turn huiskopenomteverhuren.nl/kennisbank/something-or-nothing to vastgoedmentor.com/resources/something-or-nothing.

More details on the RewriteRule can be found at https://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewriterule

As all rules point to a different domain there is no risk to run into a loop. But it is still a good idea to add the END flag to all rules to avoid the remaining rules to be evaluated. Other than the L flag, the END flag also avoids evaluation any additional rules in .htaccess files.

More details on the flags can be found at https://httpd.apache.org/docs/current/rewrite/flags.html

Michael
  • 36
  • 5
  • Thank you @Michael. However when I add the rule to the .htaccess file it doesn't do anything. No redirects happen. I tested it on a fresh browser. a redirect from http to https on the old site works using a rewrite rule. But your rules don't seem to have any effect. – Andreas Heck Jan 03 '21 at 10:15
  • Can you post what the apache error logs say? (You may need to increase the log level for mod_rewrite first, though: `LogLevel alert rewrite:trace6`) – Michael Jan 03 '21 at 10:24
  • i checked error logs but they are empty. How would I increase the log level in a CPanel environment? – Andreas Heck Jan 03 '21 at 10:32
  • The LogLevel can only be increased in you appache conf files on the server side, not in .htaccess (see https://stackoverflow.com/questions/23054592/htaccess-loglevel-not-allowed-here) I don't know cPanel, but I found this: https://www.techwalla.com/articles/how-to-edit-httpdconf-in-cpanel – Michael Jan 03 '21 at 10:46
  • Actually, you should remove the hostname from the regexp: `RewriteRule ^/kennisbank(/.*)?$ https://vastgoedmentor.com/resources$1 [END,R=301]` (and the same for all other specifc redirects as well as the general one) – Michael Jan 03 '21 at 10:48
  • This works for the redirect of the homepage but not for the pages that have a new permalink – Andreas Heck Jan 03 '21 at 10:57
  • Let us [continue this discussion in chat](https://chat.stackexchange.com/rooms/117973/discussion-between-andreas-heck-and-michael). – Andreas Heck Jan 03 '21 at 10:57
  • 1
    These directives will never work in a `.htaccess` context (as stated in the question) because of the slash prefix on the URL-path you are trying to match. You need to remove the slash prefix. eg. `^/kennisbank(/.*)?$` should be `^kennisbank(/.*)?$` (or move the directives to the vHost). In `.htaccess` (_directory_ context), the URL-path that is matched by the `RewriteRule` _pattern_ is less the directory-prefix, unlike when the directives are in the main server config (or vHost) when it always matches against the root-relative URL-path (starting with a slash). – MrWhite Jan 03 '21 at 19:17
  • "As all rules point to a different domain there is no risk to run into a loop." - It's only because the new domain is hosted on a different _server_ that the last directive won't result in a redirect-loop (dependent on the change mentioned in my comment above). But this information was not made known until long after this answer was initially written. This statement just needs updating after having updated the directives. – MrWhite Jan 03 '21 at 19:30