htaccess - rewrite rule
I'm working with the mod rewrite but I can't seem to get anything working.
All I manage is no result at all or a server 500
Basically, I want to take a bunch of dynamically named subfolders where a php file resides, and in the URL only show the site domain and the php filename
I.E.
Take
ww.domain.com/folderone/foldertwo/folderthree/scripts.php
To
ww.domain.com/scripts.php
The other thing is folderone is always the same name, but foldertwo folderthree are different names depending on user. So I guess ([a-zA-Z]) is required but I just can't get it.
Cheers
pr0gr4mm3r posted this at 18:18 — 17th June 2008.
He has: 1,502 posts
Joined: Sep 2006
Can you post what you have so far? What did you want to do with those folder directories? Here is a line that would take those folder names and pass them as $_GET elements.
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/scripts.php?$ scripts.php?folderone=$1&foldertwo=$2&folderthree=$3 [L]
greg posted this at 19:02 — 17th June 2008.
He has: 1,581 posts
Joined: Nov 2005
Maybe I'm doing something wrong. That code you gave does nothing too.
I don't really have anything "so far" that's my problem. All I have is examples I get from different sites. And so far every code has either done nothing, served a 500 or page not found.
I basically want to remove the folder names from the url. There is no $_GET info, just basic folders and a php file in the last folder.
So this page:
ww.domain.com/folderone/foldertwo/folderthree/scripts.php
Would show in users URL this:
ww.domain.com/scripts.php
So taking your code and changing to remove the GET, I would have "guessed" something like this;
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/scripts.php scripts.php [L]
Thus taking any folder name a-Z0-9 ending with / and having scripts.php file in the last one, and changing the URL to just show scripts.php
pr0gr4mm3r posted this at 19:05 — 17th June 2008.
He has: 1,502 posts
Joined: Sep 2006
Ok...so what I have is backwards then. Try something like this:
RewriteRule ^scripts.php$ /folderone/foldertwo/folderthree/scripts.php [L]
If you are getting no success running it, you need these lines before any rules:
RewriteEngine on
RewriteBase /
This may help you as well: mod_rewrite Cheat Sheet
greg posted this at 19:25 — 17th June 2008.
He has: 1,581 posts
Joined: Nov 2005
Nothing happens. scripts.php loads (small text echoed) but nothing else changes
I tried all combinations of with and without these before the rule
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
Thanks for the ideas. Is there something else I should be checking? I would have thought by now all the different rules I have tried SOMETHING would have worked in the URL.
I did run a test script and rewrite mod was on on server level.
I downloaded that cheat sheet thanks. When I need more complex things it will be useful.
I thought removing folder names from listing in the URL for a certain file would be simple enough, what with all the complex things this rewrite can do.
pr0gr4mm3r posted this at 20:25 — 17th June 2008.
He has: 1,502 posts
Joined: Sep 2006
Here is some code that worked on my server.
RewriteEngine on
RewriteBase /
RewriteRule ^test.php$ /folderone/foldertwo/folderthree/test.php [L]
If someone were to type www.example.com/test.php, they will be served the script located at www.example.com/folderone/foldertwo/folderthree/test.php from the server.
greg posted this at 21:08 — 17th June 2008.
He has: 1,581 posts
Joined: Nov 2005
And there were the magic words! It now works.
How? well, I wasn't going to explain the problem, but it might give you a laugh.
I was typing in the ww.example.com/folderone/foldertwo/folderthree/test.php
and expecting the htaccess to change it to www.example.com/test.php
Ok, so now I understand which way round rewrite works I should be able to figure dynamic content.
Cheers pr0gr4mm3r
pr0gr4mm3r posted this at 02:13 — 18th June 2008.
He has: 1,502 posts
Joined: Sep 2006
I saw those typos, but didn't mention it because I thought you just messed it up in the post. Glad you got it working.
greg posted this at 22:28 — 17th June 2008.
He has: 1,581 posts
Joined: Nov 2005
Hmm, maybe the dynamic options is not possible?
so changing these BOTH with the SAME rewrite rule:
ww.domain.com/bob/May/his.php
ww.domain.com/mary/June/hers.php
How would I go about serving php pages from different dir and file names?
I'm thinking of the idea of using $_GET on a seperate script file.
his.php and her.php in the above examples wouldn't work, but on my site thankfully I actually name the users file with sufficient data to find where their full file location would be.
So the re-write rule would actually process GET info on the script file which would serve the php file from the correct location with the new name.
pr0gr4mm3r posted this at 02:20 — 18th June 2008.
He has: 1,502 posts
Joined: Sep 2006
So...you have a separate directory structure per user? There is going to have to be something sent in the URL that will identify the user...unless you store that in the backend (PHP session or cookie).
URL rewrites in your .htaccess file won't be able to detect what the user is signed in and redirect accordingly. That will have to be done with your scripting.
Michael James Swan posted this at 22:37 — 17th June 2008.
He has: 400 posts
Joined: May 2008
I can see the problem here but the answer does not come to me...
If you take a look on www.hotscripts.com
i am sure you may find something there of use, try using google to take a look around. I am also sure that someone else here may be able to help.
Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.