[edited by: engine at 11:56 am (utc) on May 2, 2011] [edit reason] corrected code [/edit]
lucy24
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):
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.)
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
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
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
11:15 pm on May 1, 2011 (gmt 0)
lucy24: I just saw your message, and will give that a try too.
hippypink
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:
^ 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:
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
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
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
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
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]
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.