Redirecting absolute path in .htaccess file

I have this site where the URL for the pages was previously something like this:

Note that there is no extension on any of these URLs, although they were .php files. It was a Wordpress site that was at the root of the domain. The wordpress site is now moved to a folder, but I would like to set up a redirect from all previous urls to the new url (for SEO reasons).

So far, I have found a workaround that is far from perfect. What I did was put this in a .htaccess file (to remove PHP extensions):

  # Turn on the rewrite engine
  RewriteEngine  on
  # If the request doesn't end in .php (Case insensitive) continue processing rules
  RewriteCond %{REQUEST_URI} !\.php$ [NC]
  # If the request doesn't end in a slash continue processing the rules
  RewriteCond %{REQUEST_URI} [^/]$
  # Rewrite the request with a .php extension. L means this is the 'Last' rule
  RewriteRule ^(.*)$ $1.php [L]

      

And then I created different files named the same as the previous url (with a .php-extension which is then removed due to my .htaccess file). At the top of each file, I pasted

  <?php header('Location: THE_NEW_URL'); ?>

      

It works, but it's ugly like Betty! I recently ran into an error due to this temporary, silly fix for my problem. I tried setting up a new page in the root folder (for example test.php). This test.php file has a stylesheet that refers to the following way (pretty standard I suppose):

     <link href="css/style.css" rel="stylesheet" type="text/css" />

      

But the test.php file cannot find this stylesheet (and yes, I'm sure I downloaded it correctly). If I go to the original file and click on it, then I see that it is trying to access

     css/style.css.php

      

You don't have to be Einstein to know that this is my .htaccess file that was raised among the wolves. But what should I do? Is there a way to make my .htaccess file so I can do something along the lines of:

     Redirect from SPECIFIC_URL to A_NEW_SPECIFIC_URL

      

Where it won't mess up my whole domain url. I like the extensions in my files. I just need a fix for these 15 old urls that have now been moved (they didn't have extensions).

I am sucking in .htaccess files and cannot find any tutorials. It sucks! I don't know how to get better. I keep on implementing solutions I don't understand!?!

+3


source to share


2 answers


I believe this will suit your needs:

   <IfModule mod_rewrite.c>
      # enable URI rewrite 
      RewriteEngine on
      # set virtual root
      RewriteBase /
      # if request is for an actual file or directory on disk,
      # then do not continue with rewrite engine
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteCond %{REQUEST_FILENAME} !-f
      # setup rewrite table
      RewriteRule ^page1$ page1.php [R=302,L]
      RewriteRule ^page2$ page2.php [R=302,L]
      RewriteRule ^page3$ page3.php [R=302,L]
    </IfModule>

      



You can find success simply by adding these two RewriteCond lines just above the existing conditions:

  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} !-f

      

+2


source


If there are only 15 old files, an easy solution might be to hard-code those 15 files.



RewriteEngine on
RewriteRule ^/?oldfile1$ /new1/path1/file1.php [R,L]
RewriteRule ^/?oldfile2$ /new2/path2/file2.php [R,L]
RewriteRule ^/?oldfile3$ /new3/path3/file3.php [R,L]
...

      

+1


source







All Articles