homepage Welcome to WebmasterWorld Guest from 107.21.187.131
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Subscribe to WebmasterWorld

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
How to rewrite urls to remove folder?
hippypink




msg:4306482
 10:50 am on May 1, 2011 (gmt 0)

I looked for a long time, including htaccess apache docs, but cant seem to find the right answer/query-for-the-right-answer.

I have file structure like this:
http://example.com/folder/subfolder/file.php

And would like to let people access that page simply via:
http://example.com/file.php

I looked at RewriteBase, but cant get that working either.

I am not very good with htaccess yet--working on it.

 

g1smd




msg:4306559
 4:47 pm on May 1, 2011 (gmt 0)

This is the common template for such a rewrite:

RewriteCond $1 !^begin-with-rewritten-server-path
RewriteRule ^what-is-in-the-URL /the-rewritten-path-inside-the-server [L]

[edited by: engine at 11:56 am (utc) on May 2, 2011]
[edit reason] corrected code [/edit]

lucy24




msg:4306696
 11:03 pm on May 1, 2011 (gmt 0)

Is there anything in the filename that tells you what subdirectory it's in? Are they all in the same subdirectory? htaccess can do a lot, but it can't search all over your site to find any and all files named "file.php". You have to tell it where to go. For example (this is one I'm actually using):

RedirectMatch 301 /paintings/blowups/(\w+)/(?:large)?(\w+)\.html$ /paintings/$1/$2.html

This means: if the user asks to go to something ending in
/paintings/blowups/{plaintext here}/{more plaintext, optionally beginning with "large"}\.html
send them instead to /paintings/{the first word}/{the second word, minus leading "large" if any}.html

Depending on your server, the second part may have to be a complete address beginning with http and domain name. (This applies to all redirects.)

In the simplest case:

RedirectMatch 301 (http://(?:www\.)?example\.com/)(file\.php)$ $1folder/subfolder/$2

That is: capture your domain name (I've added an optional www.), capture the page name, insert "folder/subfolder/" beween the two, and send the user on their merry way.

If you've really got two layers of subdirectories, you may want to add lines for people who type one or the other but not both. And if your files are distributed among many different directories, you'll have to write specific rules for each set. For example

RedirectMatch 301 (http://(?:www\.)?example\.com/)((file1|file2|file3)\.php)$ $1folder1/subfolder1/$2

RedirectMatch 301 (http://(?:www\.)?example\.com/)((file4|file5|file6)\.php)$ $1folder1/subfolder2/$2

and so on. The "301" is for the use of search engines, so they know it's all the same page. Users don't see any difference. If you leave it out, it defaults to 302 and the search engines think you're talking about two different pages, one of which is temporarily being routed elsewhere. But that's a different forum :)

hippypink




msg:4306697
 11:13 pm on May 1, 2011 (gmt 0)

I tried that, but must be something wrong. I tried putting it at the top of the htaccess file at my site root, as well as the subfolder here:

http://example.com/folder/subfolder/.htaccess

And here is the rule i setup:

RewriteEngine On
RewriteCond $1 /folder/subfolder/index.php
RewriteRule ^/subfolder/index.php ^/folder/subfolder/index.php [L]

I tried a bunch of variations as well, but still no go.

hippypink




msg:4306698
 11:15 pm on May 1, 2011 (gmt 0)

lucy24: I just saw your message, and will give that a try too.

hippypink




msg:4306701
 11:19 pm on May 1, 2011 (gmt 0)

probably helps to know my purpose here:

I am trying to get a bunch of url's setup, like this:

example.com/campaign1
example.com/campaign2

and instead of having 100's of these in the site root, which would make a big mess and difficulty in finding anything, i thought it best to put them all in a folder like:

example.com/allmycampaigns/campaign1
example.com/allmycampaigns/campaign2

Each of the campaign folders have multiple files (a microsite). Am I going about this the best way?

hippypink




msg:4306703
 11:29 pm on May 1, 2011 (gmt 0)

To clarify, when a users visits:

example.com/campaign1
example.com/campaign1/contact.php

they see the contents of:

example.com/allmycampaigns/campaign1
example.com/allmycampaigns/campaign1/contact.php

respectively

lucy24




msg:4306726
 12:35 am on May 2, 2011 (gmt 0)

First make sure you understand the terminology.

^ without trailing space means "the beginning of the string you're matching"
$ without leading space means "the end of the string you're matching"
--with space, it would mean that the text to be matched has a space in that location
() means "hold on to this text because you'll be using it later"
--the technical term is "capture"
$1, $2 etc. means "insert the part you captured earlier, in the order they were originally captured"
--I don't know how high htaccess lets you count, but you should be safe up to $9

So if all your subdirectories are really named "campaign"-something:

RedirectMatch 301 (http://(?:www\.)?example\.com/)campaign $1allmycampaigns/campaign

If the names start with the same element, you don't need to capture them (with parentheses). You don't actually need to capture the first part (example.com) either; I just did it to save space.

The other approach is

RedirectMatch 301 !example\.com/allmycampaigns/(.+) http://www.example.com/allmycampaigns/$1
or
RedirectMatch 301 !example\.com/allmycampaigns/(.+) /allmycampaigns/$1

depending on how persnickety your server is.

This version means "if the request doesn't contain the segment 'example.com/allmycampaigns/' then insert 'allmycampaigns/' after 'example.com/' and then put back the rest of the address".

g1smd




msg:4306748
 1:42 am on May 2, 2011 (gmt 0)

This RedirectMatch code isn't useful for the current problem. Actually, the discussion about RegEx patterns is very useful, but the OP needs a RewriteRule here. The OP wants an internal rewrite, not an external redirect.

My original code included !^ meaning does NOT begin with. That is the key to getting the original code working.

lucy24




msg:4306756
 2:19 am on May 2, 2011 (gmt 0)

Oops. OK, let's globally replace "Redirect(Match)?" in everything I posted with "RewriteCond %{REQUEST_URI}", and replace all mid-line spaces with "RedirectRule". Or something like that. (hippypink, do not repeat do not attempt to interpret this literally!)

Where's the "excuse me while I go crawl into the nearest hole" smiley when I need it?

g1smd




msg:4306838
 7:53 am on May 2, 2011 (gmt 0)

No problem. Most people initially misunderstand the difference between an external redirect and an internal rewrite. So, all the RegEx examples were very useful, just use them with RewriteRule instead of RedirectMatch.

Additionally, when used in .htaccess RewriteRule cannot see the leading slash of the requested URL. The path is also localised to the current directory.

g1smd




msg:4306969
 4:28 pm on May 2, 2011 (gmt 0)

The very general version of the code is:

RewriteEngine On
RewriteCond $1 !^folder
RewriteRule ^(.*) /folder/$1 [L]

jonreynolds




msg:4309828
 12:57 pm on May 9, 2011 (gmt 0)

Hi I am trying something similar.

I have (currently on local machine):
http://localhost/projects/gallerylite/index.php?w=./album1

What I want to make happen is that the user can enter (or click a link):
http://localhost/projects/gallerylite/album1

and get to the same page as with the index.php?w=...

I have created a .htaccess file, which is being read, but cannot get the rule right. Most of the time I just see a directory listing for that folder (album1).

Here is where I currently am:


Options +FollowSymlinks
RewriteEngine On
RewriteRule ^(gallerylite/)(.*)$ $1index.php?w=$2


p.s. I am using XAMPP but will be uploading to a linux webserver.

Thanks!

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved