0

I've created a .htaccess file for my site running on my own server, Apache Ubuntu 22.04, where everything works fine except for Rewrite Rule. I definitely have mod_rewrite turned on as when I created a php file with phpinfo(); it appears in the loaded modules. I have also gone into the apache.conf and sites-enabled conf files and turned AllowOverride to All.

the Redirect Rule works completely fine with no issue yet whenever I try to use the Rewrite Rule it never seems to work.

This code works:

Redirect /foobar/ https://example.com

However this code doesn't:

RewriteEngine on
RewriteRule ^foobar/$ https://example.com/ [R]

This is what I have changed in my apache2.conf file

<Directory /var/www/>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
        Order allow,deny
        allow from all
</Directory>

Would anyone know why this could be, as nothing I've tried works and any help would be greatly appreciated.

1
  • 1
    In what directory is the .htaccess file? Do you have any other directives in the .htaccess file? (Do you have any other .htaccess files in subdirectories?) By "doesn't work" you mean it's literally not doing anything? No error? Presumably typing some "nonsense" in the .htaccess breaks with a 500 error? What is the DocumentRoot? I would not expect this to be /var/www/ (on later versions), more likely to be /var/www/html (however, it would be preferable to customise this further in a vHost). If the .htaccess file is not in the docroot then the RewriteRule will not match.
    – MrWhite
    Oct 4, 2022 at 12:43

1 Answer 1

1

You may be missing ? in your rule and the lack of an L flag beside the R may be causing issues. Generally, when redirecting from one directory to another, you would do something like this:

RewriteEngine On
RewriteRule ^foobar/?$ https://example.com/ [R,L]

However, if you are redirecting to the same domain, then it's generally better (but not required) to exclude the domain name. You can do something like this:

RewriteEngine On
RewriteRule ^foobar/?$ / [R,L]

In the event that you want to have the Uri string after foobar/ to be used with the main domain, which is useful if you're changing your Url scheme from something like /wordpress/2022/10/05/hello-world to /2022/10/05/hello-world, then you can preserve the string like this:

RewriteEngine On
RewriteRule ^foobar/(.*)$ /$1 [R=301,NC,L]

Be careful with the [R] in your rewrite rule. If you do not include the HTTP code, Apache will default to 302, which is a temporary redirect. If the Url structure is going to change permanently (or for more than a year), it would be better to specify a permanent redirect, which is 301.


Notes about the RewriteRule flags:

Flag Meaning
R Perform a redirect
NC Do not pay attention to the case of the Url
L Stop processing additional rules

If you do not have an L in the RewriteRule definition but have more rules afterwards, then Apache may just keep on going.

1
  • Nope, it didn't seem to work. I've got a feeling I've done something wrong in my apache.conf file but I've no idea what.
    – Joe Brown
    Oct 4, 2022 at 4:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .