0


I have the following htaccess code already.

RewriteEngine On
RewriteRule ^([a-zA-Z0-9-]+)/?$ index.php?page=$1

I would like that the htaccess redirect to

oszoczki.atwebpages.com/blog

when I type only

oszoczki.atwebpages.com

Richard
  • 38
  • 5
  • 1
    Possible duplicate of [Reference: mod\_rewrite, URL rewriting and "pretty links" explained](https://stackoverflow.com/questions/20563772/reference-mod-rewrite-url-rewriting-and-pretty-links-explained) – Isaac Feb 12 '19 at 12:28

1 Answers1

2
RewriteEngine On
RewriteRule ^$ /blog [R=301,L]
  • The ^$ in RewriteRule matches an empty URI and redirects to /blog.
  • R=301 represents a permanent direction.
  • L represents the last rule to be applied to this and ignore all others below it.

Update:

You can remove the L flag since redirection would have happened anyway as mentioned by @arkascha in the comments.

nice_dev
  • 12,080
  • 2
  • 18
  • 32
  • This will only work inside a dynamic configuration file (".htaccess" style file), but not in the http server host configuration where such rules preferably belong. And the `L` flag should be replaced by the `END` flag for typical situations, though here it is actually completely obsolet, since a redirection automatically terminates the rewriting process for obvious reasons. – arkascha Feb 12 '19 at 17:06
  • @arkascha OP asked about doing this in `.htaccess`, hence mentioned it. Yes, I agree that [L] is redundant though. – nice_dev Feb 12 '19 at 19:16
  • 1
    Sure, correct the OP says so. Though... most people configuring an apache http server just assume that this is the way to do it (since everyone does it) without realizing that it is only a last solution if all prior and superior alternatives cannot be applied. I personally prefer to implement rewriting rules such that they work likewise in the real configuration _and_ in dynamic configuration files. That makes things more robust and allows to move solutions between systems. – arkascha Feb 12 '19 at 19:57
  • @arkascha Makes sense. Do you mean adding those in files like `httpd.conf`? – nice_dev Feb 12 '19 at 20:10
  • 1
    Sure, absolutely. That is where your http server's host configuration belongs to (though the exact file name depends on your type of distribution, they have different philosophies how to split their files). Dynamic configuration files (".htaccess") are only provided as a last option for those who do NOT have access to their host configuration (read: really cheap hosting providers). Dynamic configuration files are hard to debug, they needlessly add complexity, they _really_ slow down the server (often for nothing) and they are a _huge_ security thread. – arkascha Feb 12 '19 at 20:33
  • 1
    The details you'd have to change here is the pattern in your rewriting rule. A `RewriteRule` is matched against the _absolute_ path in all situations, _except_ in dynamic configuration files. So a pattern of ´^$´ will never match, _unless_ in a dynamic configuration file. So what I use is `^/?$` ;-) – arkascha Feb 12 '19 at 20:36