Forum Moderators: phranque

Message Too Old, No Replies

.htaccess mod rewrite rule remove directory or folder name from url

.htaccess mod_rewrite rule remove directory or folder name from url

         

Joe_Bloggs

4:10 pm on Nov 23, 2011 (gmt 0)

10+ Year Member



Hi there,

I have a site that has many .html pages in the root folder. In order manage them, I have put them into folders. When accessing a page the URL looks like the following examples:

www.example.co.uk/folder/page.html

www.example.co.uk/folder/anotherfolder/anotherpage.html

I would like a rewrite so that when the above pages are accessed the URLs are permanently (R=301) redirected and rewritten as:

www.example.co.uk/page.html

www.example.co.uk/anotherpage.html

I have used the following code:

RewriteRule ^folder/(.+)$ http://www.example.co.uk/$1 [R=301,L]

However, this is just redirecting rather than rewriting the URL.

Any help would be much appreciated,

Many thanks!

lucy24

8:56 pm on Nov 23, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You're going to need to come back and explain what you mean by "Just redirecting rather than rewriting". Ordinarily people would say it the other way around.

A redirect means that the user is "really" at the new location, and it is visible in their address bar.

A rewrite means that the content is taken from location A while the user's address bar says location B. (A simple example that everyone has met is the 404 page.)

The URL that the user clicks or types should correspond to what you want them to see in their address bar. Rewrites happen behind the scenes.

When rewriting, make sure that all links on the "real" page are site-absolute rather than relative, or the user's browser will not be able to find them.

Joe_Bloggs

10:43 pm on Nov 23, 2011 (gmt 0)

10+ Year Member



Hi Lucy24,

Thanks for you response!

"Just redirecting rather than rewriting" - what I meant by that was the following:

I don't want the user to be simply redirected, I just want the folder to be removed from the URL. With the rewrite code I was using above, when the following was URL was entered:

www.example.co.uk/folder/page.html

The user was taken to:

www.example.co.uk/page.html

Which did not exist, and so returning a 404 error.

I still want to user to be take to:

www.example.co.uk/folder/page.html

But I want the URL to read:

www.example.co.uk/page.html

I was trying to use a 301 so that the search engines did not see duplicate content.

Thanks for all you help so far!

Joe_Bloggs

9:36 pm on Nov 24, 2011 (gmt 0)

10+ Year Member



Hi Lucy24 do you have anymore thoughts on a solution at all?

g1smd

10:45 pm on Nov 24, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



RewriteRule cannot make a new URL. YOU make the URL by creating a link to the URL you want people to see and use. RewriteRules kick in only after that link has been clicked.

You need a RewriteRule to rewrite the request made after the link was clicked to fetch the content from the real file inside the server that actually contains the content.

Separately you need a RewriteRule configured as a redirect such that when the old URL is requested, the user is told to make a new request for the new URL. You have this part in place, you now need to action the other two parts.

Joe_Bloggs

9:06 am on Nov 25, 2011 (gmt 0)

10+ Year Member



Thanks g1smd! Do you know what code I would need to use to achieve this? This is what I am struggling with at the moment.

Joe_Bloggs

9:06 am on Nov 25, 2011 (gmt 0)

10+ Year Member



Thanks g1smd! Do you know what code I would need to use to achieve this? This is what I am struggling with at the moment.

lucy24

10:41 am on Nov 25, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Is the name of the folder deducible from the name of the file? That is, deducible by the computer parsing your htaccess. If not, you will need to reroute everything via a php-type thing that looks up the name of the folder. Once you've removed the folder name by redirecting, it's gone, as if it never existed; there's no secret storage bin where htaccess can pull it out again.

Which raises the obvious question: Where are those complete URLs-- the ones with folder names-- coming from? If they're already plastered all over the internet it may not be worth the bother of changing. But if you're primarily dealing with your own internal links, then you need to change all those links so they give the name that you want the user to see. Save the redirects for the people coming in from outside with an outdated address.

The Rewrite will look just like a Redirect except of course that it won't have the [R=301] flag. And before you go "live" with the Rewrite, you will have to go back and add a Condition to the existing redirect, looking at THE_REQUEST. Otherwise you will end up in a perpetual loop.

g1smd

11:17 am on Nov 25, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Do you know what code I would need to use to achieve this?


Yes. Since this redirect/rewrite question comes up several times per week and the forum is almost ten years old, there are quite literally thousands of previous code examples to get started with.

Joe_Bloggs

1:28 pm on Nov 25, 2011 (gmt 0)

10+ Year Member



Hi g1smd, Thanks for getting back to me. I've been crawling the forum to find the answer but haven't been able to find something that works. Do you have a link to point me in the right direction?

Many thanks!

Joe_Bloggs

1:34 pm on Nov 25, 2011 (gmt 0)

10+ Year Member



Hi Lucy24, everything is in my root folder. rather than having hundreds of pages in the root to achieve www.example.co.uk/page.html I would like to put my pages into folders, but strip out the folder name from the URL.

If I have the following folders with files set in them:

Root>>holidays>>wales-holidays.html
Root>>holidays>>Wales>>cardiff-holidays.html
Root>>holidays>>Wales>>swansea-holidays.html

I would want the URL for the above files to appear in the URL as:

www.example.co.uk/wales-holidays.html
www.example.co.uk/cardiff-holidays.html
www.example.co.uk/swansea-holidays.html

Do you know what .htaccess code I would need to use to achieve this?

Many thanks,

JB

lucy24

2:00 pm on Nov 25, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Taking the folder name out is trivial, as you've already found. The hard part is putting it back in for the rewrite. Your server's knowledge base is, let's say, an inch wide and a mile deep. It doesn't know whether Cardiff is in Wales or Baluchistan. You will have to tell it, even if it had access to the knowledge just a microsecond ago when you asked it to throw out the folder name.

It is always gratifying to find something with a shorter memory than your own.

Joe_Bloggs

5:58 pm on Nov 25, 2011 (gmt 0)

10+ Year Member



Thanks Lucy24! That's exactly what I'm struggling with! What code would I use to remove the folder name but have the url go to the correct page?

I would really appreciate any help especially with a point in the right direction regarding coding...

lucy24

10:45 pm on Nov 25, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You can't do it in htaccess unless your total list of files is small enough that you can code them all manually, like

RewriteRule ^(cardiff|swansea|llllwgyww) http://www.example.com/wales/$1-holidays.html [L]

Otherwise you will need to rewrite everything to a php or similar file that looks up the location for you.