How to rewrite http get request with .htaccess?

by jaycee_rowe , in category: SEO , a year ago

How to rewrite http get request with .htaccess?

Facebook Twitter LinkedIn Telegram Whatsapp Pocket

1 answer

by dejuan.watsica , a year ago

@jaycee_rowe 

In order to rewrite an HTTP GET request using .htaccess, you can use the RewriteRule directive provided by Apache's mod_rewrite module. Here's an example of how you can achieve it:

  1. Make sure you have the mod_rewrite module enabled in your Apache server. You can check if it's enabled by looking for the following line in your Apache configuration file (usually located in /etc/apache2/apache2.conf or /etc/httpd/httpd.conf): LoadModule rewrite_module modules/mod_rewrite.so If the line is commented out with a '#' at the beginning, remove the '#' to enable it. Then, restart your Apache server.
  2. Create or edit an .htaccess file in the root directory of your website. If it doesn't already exist, you can create a new file called .htaccess in the same directory as your index.html or index.php file.
  3. In your .htaccess file, add the following code: RewriteEngine On RewriteRule ^old-page$ /new-page [L,R=301] This will redirect any request for "old-page" to "new-page". Replace "old-page" and "new-page" with the desired URL path for your redirection. The square brackets '[L,R=301]' stand for: 'L' means that this is the Last rule to be applied (no further rules will be processed). 'R=301' means that it's a 301 Permanent Redirect (you can also use 'R=302' for a temporary redirect).
  4. Save the .htaccess file.


Now, when someone tries to access the "old-page" URL, they will automatically be redirected to the "new-page" URL. Make sure to replace "old-page" and "new-page" with your specific URLs.