Using Mod_Rewrite in HTTPD.CONF file

25,693

First you need to make sure mod_rewrite is loaded. This line should be uncommented in your httpd.conf:

LoadModule rewrite_module modules/mod_rewrite.so

Then you can use these rules:

RewriteEngine On
RewriteCond %{QUERY_STRING} (^|&)thiscontent=2660(&|$)
RewriteRule ^/?applications/newWeb/www/index\.php$ http://www.example.come/index.php/publications/finance-and-economics/departmental-resources? [L,R=301]

And yeah, you'll need to setup each one specifically, so you'll need 30 of those (but only need RewriteEngine On once. You can also put these rules in an htaccess file in your document root.

Share:
25,693
mikelovelyuk
Author by

mikelovelyuk

Updated on July 09, 2022

Comments

  • mikelovelyuk
    mikelovelyuk almost 2 years

    I want to rewrite URLs so when a user goes to;

    http://www.example.com/applications/newWeb/www/index.php?page=48&thiscontent=2660&date=2013-10-11&pubType=0&PublishTime=09:30:00&from=home&tabOption=1

    and if the URL contains thiscontent=2660 (which in this example above, it does) I want to redirect them to;

    http://www.example.come/index.php/publications/finance-and-economics/departmental-resources

    I have about 30 different thiscontent=XXXX types and imagine I’ll have to copy and edit this rule 30 different times for any links to my old website still knocking around out there.

    I have access to my httpd.conf file but have never done a mod_rewrite before.

    I also don't really need these showing up in the error logs as 301s. Will that happen? Because at the moment there are hundreds!

  • Ravi K Thapliyal
    Ravi K Thapliyal over 10 years
    +1 Jon. I would recommend that you suggest OP to use thiscontent=(2660|1234|..) to avoid repeating the rule if target URL is the same.
  • mikelovelyuk
    mikelovelyuk over 10 years
    Can you expand on the (^|&) and (&|$) syntax please? Or tell me what this is called so I can look it up and learn it? I have not used it before..
  • Jon Lin
    Jon Lin over 10 years
    @MikeLovely The (<something>|<something else) means one thing or the other. The (^|&) means the beginning of the string, or a &, while the (&|$) means a & or the end of the string.
  • mikelovelyuk
    mikelovelyuk over 10 years
    Do I have to wrap my rewrite rules in a <IfModule mod_rewrite.c> block?
  • Jon Lin
    Jon Lin over 10 years
    @MikeLovely no, that's only if you wanted to port your stuff to another server that doesn't have mod_rewrite loaded
  • mikelovelyuk
    mikelovelyuk over 10 years
    I did a test. When I go to the page above it now redirects to http://www.example.com/index.php/publications/finance-and-ec‌​onomics/departmental‌​-resources[L,R=301]?‌​page=48&thiscontent=‌​2660&date=2013-10-11‌​&pubType=0&PublishTi‌​me=09:30:00&from=hom‌​e&tabOption=1 - which is wrong. I need to remove everything after the word resources from the URL string
  • mikelovelyuk
    mikelovelyuk over 10 years
    @JonLin Yes. That was the issue. Thank you. I've added in two more rules now. and I've put one at the end that redirects to just http://www.example.com and I removed the (&|$) from the end of the RewriteCond. That way, until I get around to doing all 30, that last one just sends them to the home page.