Some of my RewriteRules don't work if i put the [R=301].

why this don't work? RewriteRule ^sitemap\.xml$ sitemap.php [R=301,L]

It shows a Forbidden error when i try to access the URL https://example.com/sitemap.xml

  • Have you checked the file permissions? it should be something 644 / 0644 – Simon Hayter Dec 30 '18 at 19:00
  • @SimonHayter i have never heard about that, how can i check it? – mind set Dec 30 '18 at 19:07
  • Lookup CHMOD permissions. – Simon Hayter Dec 30 '18 at 21:18
RewriteRule ^sitemap\.xml$ sitemap.php [R=301,L]

Note that sitemap.php is a relative path - it does not start with a slash (root-relative) or scheme+hostname (absolute). Relative to what?

By itself, this won't work because in a per-directory context (ie. .htaccess), Apache prepends relative path substitutions (ie. sitemap.php) with the directory-prefix (ie. the absolute filesystem path that leads to the .htaccess file. If the .htaccess file is in the document root, then it prepends the document root.)

In other words, it will result in a malformed external redirect to a URL of the form:


Instead of the likely intended URL https://example.com/sitemap.php. (Although, whatever URL is being redirected to should be clearly visible in the browser's address bar.)

/home/user/public_html is whatever your directory-prefix is. This will likely result in a 404 or a 403 (as in your case), depending on your file structure and server config.

Now, before you added the R=301 flag, this was an internal rewrite - and it would have been correct to prepend the directory-prefix in this instance since an internal rewrite is internal to your server.

To redirect to the intended URL (ie. https://example.com/sitemap.php), you need to either prefix the substitution string with a slash, to make the URL-path root-relative (or include the scheme+hostname to form an absolute URL). For example:

RewriteRule ^sitemap\.xml$ /sitemap.php [R=301,L]


RewriteRule ^sitemap\.xml$ https://example.com/sitemap.php [R=301,L]

OR, set the RewriteBase directive appropriately:

RewriteBase /
RewriteRule ^sitemap\.xml$ sitemap.php [R=301,L]

The RewriteBase directive overrides the directory-prefix for relative path substitutions.

Some of my RewriteRules don't work if i put the [R=301].

Yes, that is quite probable. Most RewriteRule directives are not intended to be external redirects. mod_rewrite (ie. RewriteRule and RewriteCond, etc.) is Apache's URL-rewriting module; it also does redirects.

RewriteRule ^sitemap\.xml$ sitemap.php [R=301,L]

Going back to your original directive - this probably should not be a "redirect". You should simply remove the R=301 flag.

You will need to clear your browser cache since 301 (permanent) redirects are cached (by default) by the browser (including the ones in error).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.