Search code examples
apache.htaccessmod-rewritehttp-redirecthttp-status-code-301

301 Redirect A URL Pattern Using .htaccess


I want to redirect my URLs to a new pattern. For this purpose, I used 301 redirect for every single URL but that are taking a huge time and my .htaccess file is going large and large as I have thousands of URLs.

So now Someone said to me to use .htaccess to use 301 redirect or rewrite engine option. Now I am new to .htaccess to use Pattern Redirect. First of all clear me out that is this possible to use 301 redirect in patterns? If yes then Can I do pattern 301 redirect in the below URLs? I want to redirect the below pattern so can you help me?

  1. /search/label/XXXXXXXXXX to /category/XXXXXXXXXX
  2. /year/month/XXXXXXXXXX.html/?m=0 to /year/month/XXXXXXXXXX.html
  3. /year/month/XXXXXXXXXX.html/?m=1 to /year/month/XXXXXXXXXX.html
  4. /search to /
  5. /feed to /

XXXXXXXXXX means some text/no that are dynamic and changeable. year and month means only no that are also dynamic and changeable. / means site homepage. Rest are fixed text.

Please keep in mind that sometime there are many variables in every URL so we also want to avoid that that always start from ?variable=value&variable=value in the end of every URL.


Solution

  • After asking here, I keep trying myself too so I am able to do it and working on my side. I added below codes in my .htaccess file and after that I am able to redirect all upper URLs without any 404 error.

    Redirect 301 /search/label http://www.example.com/category
    Redirect 301 /search http://www.example.com
    Redirect 301 /feed http://www.example.com
    

    For 2,3 URL pattern, I did nothing because after checking, its not showing any 404 error as they are only variable in front of URL so no need to edit that.