Zeth
Zeth

Reputation: 2618

Redirecting an absolute path in a .htaccess file

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

Please note, that there's no extension on any of these URL's even though that they were .php-files. It was a Wordpress-site that was in the root of the domain. The wordpress-site has now been moved into a folder, but I would like to setup a redirect from all the previous URL's to the new URL's (for SEO reasons).

So far I found a temporary solution that is far from perfect. What I did was that I put this in the .htaccess-file (to remove the 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've created the different files called the same as the previously URL's (with the .php-extension, which then is removed due to my .htaccess-file). In the top of each file, I have inserted a

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

It works, but it's ugly as Betty! Recently, I encountered an error due to this temporary, stupid fix to my problem. I tried to setup a new page in the root of the folder (called test.php). This test.php-file has a stylesheet, that it links to the following way (pretty standard, I assume):

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

But the test.php-file can't find this stylesheet (and yes, I'm sure I uploaded it correctly). If I go to the source file and click it, then I can see that it tries to access

     css/style.css.php

You don't have to be Einstein to know, that it's my .htaccess-file that has been raised amongst wolfs. But what should I do? Are 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 doesn't screw up all URL's on my domain. I like have extensions on my files. I just need a fix for these 15 old URL's that has now been moved (that didn't have extensions).

I suck at .htaccess-files and can't find any tutorials anywhere. It sucks! I don't know how to get better at it. I keep implementing solutions that I don't understand!?!

Upvotes: 3

Views: 5893

Answers (2)

Nathan
Nathan

Reputation: 1700

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 might find success by simply adding these two RewriteCond lines just above your existing conditions:

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

Upvotes: 2

Olaf Dietsche
Olaf Dietsche

Reputation: 74078

If there are just 15 old files, a simple solution could be to hardcode these 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]
...

Upvotes: 1

Related Questions