Redirect any URL html extension to a specific page

Asked

Viewed 846 times

0

Please I have a question on . htaccess (or apache).

I need to redirect any url that contains extension .html or .htm to a specific url. Example:

  • domain.com/test.html -> domain.com/page
  • domain.com/test123.htm -> domain.com/page

Follows the current .htacces:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

Thanks for the help.

1 answer

1


For this redirection it is necessary to have the module mod_rewrite connected. To turn it on, use this command:

sudo a2enmod rewrite
sudo service apache2 restart # para reiniciar o servidor

Inserts the following content into .htaccess:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

RewriteRule ^pagina$ - [L] 
RewriteRule ^(.*)\.html?$ pagina [L] 

RewriteRule ^index\.php$ - [L] 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule . /index.php [L] 
</IfModule>

Note: for this to work, you have to have the option AllowOverride All in the directory configuration(<Directory>...</Directory>) of vhost in force.

  • Hello Tim, thank you. It didn’t work. I’m using inside Wordpress. Allowoverrride All was already configured. He ignores the line, which means he’s not redirecting, he drops right into a 404.

  • Maybe because you already have . htaccess configured by default. Check that there are no other Rewriterule rules in your . htaccess or apache configuration.

  • I put in the original post the current . htaccess

  • I edited the contents of . htaccess from my answer as per the situation. This should work.

  • Thank you! That’s right.

  • Just for the record, this command does not let you enter the WP /wp-admin.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.