1

Read this and bababa but sadly none of them can direct me how to debug.

I am trying to redirect below url https://example.com/hello to https://example.com/hello.html

Here is how I did

First

vim /etc/apache2/apache2.conf, set AllowOverride All in <Directory /var/www/>

Now is

<Directory />
        Options FollowSymLinks
        AllowOverride None
        Require all denied
</Directory>

<Directory /usr/share>
        AllowOverride None
        Require all granted
</Directory>

<Directory /var/www/>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
</Directory>

Second

sudo service apache2 restart

Third

Create .htaccess in public_html and below code

<IfModule mod_rewrite.c>
  
Options FollowSymLinks

RewriteEngine On
RewriteCond %{REQUEST_URI} !^.*\.html$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ %{REQUEST_FILENAME}.html

</IfModule>

But still return 404 not found like below.

enter image description here

Any suggestion or any parts I am missing to config apache? Thanks


------ANSWER------

Missed the configuration for mod_rewrite. Can check this post

Pak Ho Cheung
  • 1,382
  • 6
  • 22
  • 52

1 Answers1

1

You are adding .html at the end of %{REQUEST_FILENAME} which will aways cause a 404 since %{REQUEST_FILENAME} represents full filesystem path not the URI path.

You should use this rule:

RewriteEngine On

RewriteCond %{REQUEST_URI} !\.html$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . %{REQUEST_URI}.html [L]

or even better:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+?)/?$ $1.html [L]
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • Hi. I still get the 404 error. Please see my updated post. I uploaded an screenshot of what I get with the 404 error. Maybe the configuration as well? – Pak Ho Cheung Jul 19 '21 at 18:53
  • Verify whether your `.htaccess` is enabled or not, by putting some garbage (random) text on top of your `.htaccess` and see if it generates 500 (internal server) error or not when you visit your page in browser. 500 error means .htaccess is enabled. – anubhava Jul 19 '21 at 19:04
  • ohh. No 500 error is shown even i put some garbage text in .htaccess. That means something wrong in apache2.conf? Please see additional information from the post. Guess maybe something wrong in user account in apache – Pak Ho Cheung Jul 19 '21 at 19:08
  • You have `AllowOverride All` inside `/var/www/`. Is this your `DocumentRoot` as well? And does `.htaccess` exist right there? – anubhava Jul 19 '21 at 19:19
  • .htaccess is in ```/var/www/hello/public_html```. So, I should add ``` Options Indexes FollowSymLinks AllowOverride All Require all granted ``` in apache2.conf? – Pak Ho Cheung Jul 19 '21 at 19:24
  • Yes and make sure `/var/www/hello/public_html` is set as `DocumentRoot` as well. After restarting Apache run that verification of 500 error again. – anubhava Jul 19 '21 at 19:29
  • for ```/var/www/hello/public_html is set as DocumentRoot``` Do you mean to set it in ```/etc/apache2/sites-available/hello.conf```? – Pak Ho Cheung Jul 19 '21 at 19:38
  • I see. I am not too :) Thanks a lot. Will try to figure it out – Pak Ho Cheung Jul 19 '21 at 19:44
  • 1
    Not really. Will notice you and test your code after configured the Apache – Pak Ho Cheung Jul 20 '21 at 07:59
  • 1
    Hi. Finally find out the reason. It is because I missed out the configuration for ```mod_rewrite``` – Pak Ho Cheung Jul 21 '21 at 13:12