Forum Moderators: phranque

Message Too Old, No Replies

htaccess step to far (for me)

redirect to non existant directories?

         

adwhite

4:26 pm on Oct 25, 2011 (gmt 0)

10+ Year Member



Hi,

I currently use htaccess to redirect non-existent html pages to php programs with parameters, for example:

RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)\.html$ phptemplate.php?page=$1 [L]
RewriteRule ^$ phptemplate.php?page=index [L]

This all works fine and dandy with never a problem, but now I have to do something different and I'm struggling.

I have 200+ unique users on a website and each one needs to access the system via an unique url eg :

www.example.com/Fred_Bloggs or www.example.com/John_Smith

this should take them to a page www.example.com/register.html where they will sign in and then ideally they would all see a site which was www.example.com/UNIQUE_NAME/index.html, www.example.com/UNIQUE_NAME/agenda.html etc etc

is this possible without creating 200+ folders?

Cheers

Andy

g1smd

5:23 pm on Oct 25, 2011 (gmt 0)

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



You talked about your code producing "redirects" but then presented code with two RewriteRules both configured as "rewrites".

Are you sure about the difference between these two things? Both use a RewriteRule, but the outcome is very different.

As coded now, your rewrites open your site to infinite duplicate content. It is not the right way to do this.

This type of code is discussed several times per week here, so it will be useful to read a number of recent threads.

adwhite

5:51 pm on Oct 25, 2011 (gmt 0)

10+ Year Member



the current code works fine, if the url doesn't have "index.html" then the second rewrite substitutes it, that's not the issue it's the bit about the unique users I need help with if, of course, what I'm asking is feasible.

Just to clarify the primary 3 lines of code is for the standard website where the site can have an unlimited number of pages taken from a database which fall within a "normal" website structure, what I need to acheive with this new requirement is the unique user directory structure, and if that means changing the 3 lines I have then that's fine.

g1smd

6:21 pm on Oct 25, 2011 (gmt 0)

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



You can add another RewriteRule looking at your extensionless requests and then rewrite or redirect then.

The RegEx pattern in this rule will be quite different to your other patterns.

Never link to named index files such as index.html in your URLs.

adwhite

10:28 am on Oct 26, 2011 (gmt 0)

10+ Year Member



I've got a version of working for virtual user directories :

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ phptemplate.php?page=$1 [L]

which seems to do what I want, is there any reason why this wouldn't work consistently?

lucy24

8:02 pm on Oct 26, 2011 (gmt 0)

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



If you don't mind the extra work of the server having to check every single request it ever receives -- including ones with extensions like .jpg or .css that would never need to be redirected -- and if your php file can distinguish between the names it's supposed to work with and the ones it should throw back unchanged but with some kind of flag to keep the whole thing from happening all over again ...

Then I'd have to say it works.

g1smd

11:52 pm on Oct 26, 2011 (gmt 0)

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



There's a very good reason not to use this code.

The (.*) pattern matches requests for pages, images, stylesheets and javascript files. For every request for every one of those things for every page of the site, mod_rewrite processing will be held up for an eternity while the operating system goes off to see if any of the requested names match a real file or folder on the server hard drive.

Of course the images, CSS and javascript files exist as files! So why are you forcing the server to go check over and over again?

Add another RewriteCond before all of the others that disallows the ruleset for all of those types of request, or change the (.*) pattern to match only the requests that will be rewritten.

adwhite

1:41 pm on Oct 27, 2011 (gmt 0)

10+ Year Member



I'd already changed it to

RewriteRule ^(.*)\.html$ phptemplate.php?page=$1 [L]

but honestly Lucy24 and g1smd if all I need was waspish critiscm and sitting on high horses I'd have asked my mother for help.
I really thought the point of this forum was to help people who didn't know how to do things, not to bask in your unhelpful glory. By all means expect people to work it out by themselves (which I have done no thanks to you) but surely some helpful guidance can't be beneath you. What I have may not be to your perfect standards but given I currently have no clue what your perfect standards are then it will have to do.

Message to Moderator: They may be good, I certainly can't tell, but I do know they don't encourage me or I suspect others to use this forum to progress.

lucy24

6:57 pm on Oct 27, 2011 (gmt 0)

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



Your mother speaks Apache? Send her over; we need people :)

This version
^(.*)\.html$

does constrain the rule, so you've cleaned up half the problem. But it doesn't save the server much work. A computer, unlike a human, can't glance ahead and see that it will be stopping before the ".html" element. It doesn't know until it gets there-- at which point it has already captured the whole request, and now has to backtrack.

The simplest form would be

^([^.]+)\.html$

g1smd

8:07 pm on Oct 27, 2011 (gmt 0)

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



In many cases
.+
is no better than
.*
for this.

Using
^([^/.]+)\.html$
parses left-to-right much faster (works only for root requests, modify for deeper folders).

Leosghost

8:38 pm on Oct 27, 2011 (gmt 0)

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



Message to Moderator: They may be good, I certainly can't tell, but I do know they don't encourage me or I suspect others to use this forum to progress.


They encourage you to work out why things happen..

or they attempt to teach you to fish..

not to hand you Filet 'O' Fish

And the vast majority of the people who visit and read this forum are extremely grateful to g1smd for his many years of answering questions here ( along with Jim )..and to lucy24 ( a more recent but extremely valuable member to this site and this forum ) for their free advice and help..

g1smd

9:38 pm on Oct 27, 2011 (gmt 0)

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



I take time out of my working day to come here to try to help a few people out, and so do perhaps three or four other people. Seven years ago this stuff was also Icelandic poetry as far as I was concerned. It took a long time to learn what is what; and that wasn't done by being handed code on a plate - Jim is a far more harsh taskmaster. Unless you fully understand the concepts you can royally screw up your site and your business with a few simple typos. With so many questions and so few volunteers to answer them, answers will be pushing you to understand the concepts. You should be reading the Apache and mod_rewrite manuals as well as as many old threads as you can. There is ten years of Apache wisdom here.

The most discouraging thing for me is that the new forum content these days is mostly the same 60 questions asked over and over again. With each one already answered several hundred times, or in some cases more than a thousand times, it is often tempting to post "did you actually search the forum?", but I do realise that many people don't actually know what question to ask, or don't ask the right question. So, answers will be given to push people to do more research, along with notes and cautions to not do things a certain way because it will land you in trouble,; all this with an eye not just to the person asking the question today but to the thousands of people who will read the thread in the next few months.

Perhaps once per month someone asks something entirely new.

adwhite

11:31 am on Oct 28, 2011 (gmt 0)

10+ Year Member



Hi Lucy24 and g1smd

Unfortunately if my mother did speak apache it is probably the native North American kind, so consequently of little use in this situation.

Thank you for the refinement suggestions, although for the purposes of this specific problem limiting the action to the root directory rather defeats the object, but it's a useful lesson in how things work.

I'm aware that you spend your own time dragging us poor horses to the trough and then watching us refuse to drink, but I can assure you I'd trawled the web looking for a solution to my problem before I resorted to posting a question, but as you so rightly say the problem often is that we don't know the right question to ask.

I can't speak for others, but for myself I use htaccess to control websites that I build, rarely do I need to change anything I just copy a working version to a new website and make any site specific changes and hey presto it works, so consequently I hardly ever need to experiment with new concepts.

I'm perfectly aware that the slightest typo will screw everything up so whilst my code may not be the best I don't have the knowledge or time to make it better, if it ain't broke don't fix it would be my policy here.

In this particular case I was faced with a problem way outside my comfort zone with a timeframe that was wildly optimistic, but then that's clients for you. So I asked for help, you can see I've been a member since 2004 and only posted 50 times, I don't tend to ask for help, I tend to sort a problem out for myself because (Leosghost) I like fishing.

I didn't find my solution on this site I found it elsewhere, I asked what I thought was a perfectly reasonable question (#:4379514) based on the fact that I had a solution, but because I don't know enough I didn't know if my solution was any good. And I got (#:4379727 and #:4379854) gee thanks guys that really helped.

I'm sure it is tedious (and here I'm not being sarcastic) that the same questions get asked again and again, (although because I don't know the right question I couldn't find an answer in this forum) so perhaps a sticky with the questions (and nupty derivatives like mine which was "htacess +"virtual directories") and then a search link to the previously given answers would be of some use. Then as people ask new "wrong" questions the sticky could be updated to point the "wrong" question towards the right answer. Something like that might enable you to spend less time on the mundane and allow you to enjoy the challenge of the new when it comes along.

Regards

Andy

g1smd

11:36 am on Oct 28, 2011 (gmt 0)

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



Now that you've got a solution that you think might fit, post it here, and let's make sure it actually does 100% what you need it to do.

if it ain't broke don't fix it would be my policy here.

In many cases, code that "appears" to work actually has various subtle problems that are slowly telling Google "this site has a technical problem, don't trust it".

adwhite

11:48 am on Oct 28, 2011 (gmt 0)

10+ Year Member



Hi,

my code is as above but with Lucy24's refinement :

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^.]+)\.html$ phptemplate.php?page=$1 [L]
RewriteRule ^$ phptemplate.php?page=index [L]

This appears to work when people are not signed in, so using a "real" directory structure (eg www.example.com/agenda.html) and also when people are signed in and using a virtual directory structure (eg www.example.com/UNIQUE_USER/agenda.html)

g1smd

12:01 pm on Oct 28, 2011 (gmt 0)

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



If I request
example.com/<does-not-exist>.html
does your PHP script correctly return the required 404 status code?

Are any of your folders called
example.com/<something>.html/
on the site? You probably don't need the "-d" check at all.

adwhite

2:01 pm on Oct 28, 2011 (gmt 0)

10+ Year Member



If someone puts in example.com/<does-not-exist>.html they get a custom error page with the navigation structure and a polite message saying the page they've requested doesn't exist please use the navigation buttons etc.
I've commented out the RewriteCond %{REQUEST_FILENAME} !-d and everything still appears to be working, but I'm continuing to test it all.

adwhite

3:00 pm on Oct 28, 2011 (gmt 0)

10+ Year Member



A couple of tweaks to get site specific bits working, but this is the current latest version.

RewriteEngine On

RewriteRule ^([a-zA-Z0-9_-]+)\.html$ phptemplate.php?page=$1 [L]
RewriteRule ^$ phptemplate.php?page=index [L]

The lines above deal with the site when it doesn't have the virtual directory


RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^.]+)\.html$ phptemplate.php?page=$1 [L]

The lines above sort out the virtual directory

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

The above line allows for the client request that the link www.example.com/Fred_Bloggs goes to the login page, they intend to send their clients a brochure with this custom login url

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

The above line redirects www.example.com/UNIQUE_USER/account.php back to the real version

Now it all seems to work but I've found one area where it doesn't work, if you put www.example.com/UNIQUE_USER/ you get url not found, but to be honest you'd only get there if you typed it rather than using the navigation system, so if someone wants to do that I can probably live with it.
I'm sure the code isn't perfect and if people want to suggest refinements I'm more than happy to give them a go, but failing that I'll probably put it to bed and move onto the next pressing issue.