Jump to content

301 Redirecting CMS database categories


tec-4

Recommended Posts

I've got a website that had quite a few links going to it specific pages and am trying to properly configure the URL to 301 redirect to the previously linked page to keep all the link juice going to it an not be lost.

 

Also, have switched CMS's and the current one does not allow for ".php" to append the URL when creating pages, so trying to do it with a 301 redirect...but can't seem to get it to work.  Is this because they are not actual web pages, but being dynamically built through a MySQL database? For example, all internal pages are using a page.php file to create the structure of all pages site-wide.

 

Old URL (what I'd like to keep): www. example .com/selling.php

New URL (which I'd like to 301): www. example .com/selling

 

Ive tried this 301 redirect but didn't seem to work:

 

RewriteEngine on

RewriteCond %{HTTP_HOST} ^example.com$ [OR]

RewriteCond %{HTTP_HOST} ^www.example.com$

RewriteRule ^selling$ "http\:\/\/www\.example\.com\/selling\.php" [R=301,L]

 

 

Anyone have any ideas or suggestions on how to go about redirecting to the selling.php page?

 

 

 

Link to comment
Share on other sites

The CMS is built upon a PHP framework, so it could handle the .php extension but it seems to be sanitizing my string input for the URL to not include periods.

 

For example, when I create a page through the CMS, i put the file extension of /selling.php and it sanitized to /sellingphp

 

So, I was wondering if I were to create a page called /selling, if i could rewrite it using the .htaccess file to turn the /selling into /selling.php  ...not sure if my logic is correct or not though, or if this would not work because the pages are being created through the MySQL database. The code that is turning my pages (categories) into actual friendly URL's are as follows:

 

RewriteEngine on

RewriteRule ^([A-Za-z0-9\-]+)$ /page.php?category=$1 [L]

 

 

Link to comment
Share on other sites

I came about the following code that should append the ".php" at the end of my extensionless url-paths, but doesn't seem to work...anyone know if why? Or if there is a reason for not working with the way my pages are created?

 

RewriteCond %{DOCUMENT_ROOT}/$1.php -f

RewriteRule ^(([^/]+/)*[^.]+)$ /$1.php [L]

Link to comment
Share on other sites

That RewriteCond requires that the .php file actually exists. Which it doesn't.

 

And about the CMS "handling" it, I meant whether you can define a page called "selling" and see it when you go to /selling.php.

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+/)*[^.]+$ $0.php [L,R]

 

If not then you'll need a second Rule: the first rewrites to a .php and redirects the browser (which is what the above does) while the second silently strips that same .php.

RewriteRule ^(([^/]+/)*[^.])\.php$ $1 [L]

 

But there's a big problem with this: if you generate a form with action=/selling and method=post then either (a) it might not work, or (b) users will be notified that they're being redirected elsewhere.

Link to comment
Share on other sites

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+/)*[^.]+$ $0.php [L,R]

 

I tried the code above, and it seemed to write it to the .php extension.  However,  after placing it into my .htaccess file, my URL's were formatted as follows:

 

http: // www. example . com/directory/directory2/directory3/selling.php

 

My initial thought was that I needed to switch the "Request Filename" to the directory before the "selling.php", but did not seem to work.

 

By the way, thanks for the heads up on the risks of constructing this like this and some things to look for.

Link to comment
Share on other sites

That shaved off the directories nicely and now the strings are formatting correct visually (/buying.php) etc. But all pages seem to be going to 404 pages.

 

Tried two sets of code (one to include the .php and one that you sent over to shave the .php off again). They look as follows:

 

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+/)*[^.]+$ /$0.php [L,R]

 

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+/)*[^.]+$ /$0.php [L,R]
RewriteRule ^(([^/]+/)*[^.])\.php$ $1 [L]

 

Not sure if I have these correct or not.  The first one adds the .php and looks correct, but shows a 404 and the latter code I could not see a visual difference from the first code (aka - formatted like /buying.php)

Link to comment
Share on other sites

The second set is the version to use.

 

Try this:

#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule ^([^/]+/)*[^.]+$ /$0.php [L,R]
RewriteRule ^(([^/]+/)*[^.])\.php$ /$1 [L,R]

What happens if you go to /buying.php?

Link to comment
Share on other sites

When using the code above, with the hash marks, and directly linking to the page example . com /buying.php it goes to a 404 error page.  With this code, I can maneuver throughout the site without any error pages (meaning all the links in the navigation bars and such don't seem to be

re-written and I am directed to the /buying page with its content. )

 

When I take out the hash marks (all 4 lines being active) all the navigation links in my site, when clicked, are re-written to take me to pages appending the .php, like /buying.php, but go to 404 errors.

Link to comment
Share on other sites

When using the code above, with the hash marks, and directly linking to the page example . com /buying.php it goes to a 404 error page.

And you aren't redirected anywhere?

 

With the hashes (ie, the three lines commented out) what's the exact contents of your .htaccess?

Link to comment
Share on other sites

Correct.  When placed in the .htaccess file and directing http:// www. example .com / buying.php it goes to that page (with the .php extension w/ no re-write and shows 404 error) and when navigating the site w/ the actual site's URL's that do not have the extension, everything thing appears as normal (aka - going to all pages like usual w/ no .php extension affixed to it)

 

.htaccess file:  Note I tried putting it before/after the page.php re-write (the thing formatting the pages) and did not seem to do anything...not sure if I need to re-organize the file or not or something..

RewriteEngine on

#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule ^([^/]+/)*[^.]+$ /$0.php [L,R]
RewriteRule ^(([^/]+/)*[^.])\.php$ /$1 [L,R]

RewriteRule ^blog$ /blog/index.php [L]
RewriteRule ^(blog/)(archives/)([A-Za-z0-9\-]+)$ /blog/archives/index.php?id=$3 [L]
RewriteRule ^blog/rss$ /blog/index.php?rss=true [L]
RewriteRule ^(blog/)([A-Za-z0-9\-]+)$ /blog/index.php?id=$2 [L]
RewriteRule ^(blog/)(entry/)([A-Za-z0-9\-]+)(/print)$ /blog/entry/index.php?id=$3&print=true [L]
RewriteRule ^(blog/)(entry/)([A-Za-z0-9\-]+)(/share)$ /blog/entry/index.php?id=$3&share=true [L]
RewriteRule ^(blog/)(entry/)([A-Za-z0-9\-]+)$ /blog/entry/index.php?id=$3 [L]

RewriteRule ^([A-Za-z0-9\-]+)$ /page.php?category=$1 [L]

ErrorDocument 404 /404.php


RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]

Link to comment
Share on other sites

Ah, the wonders of eating lunch.

 

Typo:

RewriteRule ^(([^/]+/)*[^.]+)\.php$ /$1 [L,R]
#                          ^ missed that

 

Once that works (should be redirected to /buying) use

RewriteRule ^(([^/]+/)*[^.]+)\.php$ $1 [L]

instead and uncomment the lines before.

Link to comment
Share on other sites

Tried just using the revised code (taking out everything except this one you've given me)

RewriteRule ^(([^/]+/)*[^.]+)\.php$ /$1 [L,R]

and it was able to load the homepage of the site (example . com)but was not able to locate any internal webpages with, or without .php extensions....just came to a blank browser page.

 

Then I tried using all 4 lines, as follows and seemed as though the same thing happened

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+/)*[^.]+$ /$0.php [L,R]
RewriteRule ^(([^/]+/)*[^.]+)\.php$ /$1 [L,R]

 

A little confused so hope I tested the correct strings.

 

 

Link to comment
Share on other sites

When placing those 4 lines, as is, in the file and try to load the site (on any webpage, including homepage) it says: "Internet Explorer cannot display the webpage"

 

I then attempted to play around with the commenting of certain lines and when I commented out everything except the last line it shows: 500 Internal Server Error and when commenting out everything, other than the 3rd line down (second from the bottom) it appends the .php to the files like so: /buying.php but goes to the 404 error page.

Link to comment
Share on other sites

That's because, as far as I can see, that code would cause an infinite loop. Using your example of /buying, it would get redirected to /buying.php, this request to /buying.php will then hit the server, getting rewritten (as opposed to redirected) to /buying, which will get rewritten to /buying.php... I'm sure you get the picture. Basically what you are trying to do is have your cake and eat it too. But hey, as I always say, you can't eat your cake unless you have it.

 

What you will need to do is to somehow prevent the rewrite from continuously looping. One way of doing this would be to append something to the url and check for it.

 

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} !rewritten=1
RewriteRule ^([^/]+/)*[^.]+$ /$0.php [L,R]

RewriteRule ^(([^/]+/)*[^.]+)\.php$ $1?rewritten=1 [L]

Link to comment
Share on other sites

Oh okay, well that make sense.  Was I supposed to pop that in as is or did I need to modify it at all?

 

When I tried the code above and when trying to access the homepage of the website it showed a 500 Internal Server Error.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.