WHAT'S NEW?
Loading...
Showing posts with label mod_rewrite. Show all posts
Showing posts with label mod_rewrite. Show all posts
As of CentOS version 7, the mod_rewrite Apache module is enabled by default. We will verify this is the case with the httpd command and -M flag, which prints a list of all loaded modules:
  • httpd -M
Output
. . . remoteip_module (shared) reqtimeout_module (shared) rewrite_module (shared) setenvif_module (shared) slotmem_plain_module (shared) . . .
If the rewrite_module does not appear in the output, enable it by editing the 00-base.conf file with the vi editor:
  • sudo vi /etc/httpd/conf.modules.d/00-base.conf
Next, apply the configuration change by restarting Apache:
  • sudo systemctl restart httpd
With Apache installed and the mod_rewrite module enabled, we're ready to configure the use of a .htaccess file.
  • sudo vi /etc/httpd/conf/httpd.conf
Locate the <Directory /var/www/html> section and change the AllowOverride directive from None to All:
/etc/httpd/conf/httpd.conf
. . .
<Directory /var/www/html>
. . .
 # 
 # AllowOverride controls what directives may be placed in .htaccess files.
 # It can be "All", "None", or any combination of the keywords:
 # Options FileInfo AuthConfig Limit
 #
 AllowOverride All
. . .
</Directory>
. . .
Save and exit the file and then restart Apache to apply the change:
  • sudo systemctl restart httpd
Next, create a .htaccess file in the default document root, /var/www/html, for Apache.
 We will see how to  use mod_rewrite to reditect url in a configured virtual host



This example is based on a url with incoming query string empty. The trick is to use a RewriteCond to match the empty query string portion of the URL, and a regular RewriteRule to match the path.



<virtualhost>
    ServerAdmin awesome@test.com
    DocumentRoot C:\test
    ServerName test.com

     <directory test="">
       Order allow,deny
       Allow from all
       AllowOverride All
    </directory>
    
    RewriteEngine On
    RewriteCond %{REQUEST_URI}  ^/web/guest/myurl/$
    RewriteCond %{QUERY_STRING} ^$
    RewriteRule ^(.*)$ http://www.mynewdomain.com/ [R=301,L]
</virtualhost>    

As result the url

http://www.mycurrentsite.com/web/guest/myurl/ will be redirect to http://www.mynewdomain.com/

the url

http://www.mycurrentsite.com/web/guest/myurl/?id=576576 will not be redirect.