homepage Welcome to WebmasterWorld Guest from 54.205.144.231
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
Is my .htaccess file OK?
rewrite question
gcan




msg:4375842
 8:56 am on Oct 18, 2011 (gmt 0)

This is my .htaccess file:


Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteRule ^classifieds/([0-9a]+)/([0-9a-z-]+).html$ classifieds.php?lan=en&cid=$1&title=$2 [NC,L]
RewriteRule ^photos/([0-9a-z-]+)/([0-9a-z-]+)/([0-9-]+)/([0-9a-z-]+).html$ photos.php?descr=$1&secc=$2&data=$3&pid=$4 [NC,L]
RewriteRule ^forum/([a-z-]+)/([a-z-]+)/([0-9-]+)/([0-9a-z-]+).html$ forum.php?secc=$1&secca=$2&title=$3&fid=$4 [NC,L]


Now I want to offer to my users profile addresses like: httt://www.mydomain.com/username/

So, I added the following lines to the .htaccess file:


RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([a-z0-9]+)\/?$ profile.php?username=$1


It works, but I want to know if it's OK and can't cause any conflicts in .htaccess file or with Apache?

I reserved all words used in .htaccess file - "classifieds", "photos", "forum" so that users can't register these words as nicknames. I reserved many other words which I may want to use as folder/directory names, so my question is not about it. I just need to know if tecnically my .htaccess file will be OK.

Thanks.

 

rocknbil




msg:4376013
 4:29 pm on Oct 18, 2011 (gmt 0)

I'll open this one, I've been spanked enough over it. :-)


RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([a-z0-9]+)\/?$ profile.php?username=$1


The effect is it searches the entire file system TWICE for the requested resource, first to see if it's a file, second to see if it's a directory, and is highly inefficient. Sadly, it's also the same bit of code used by almost every CMS or blog software out there.

Please see post #4274745 in this thread [webmasterworld.com] for one way to fix this. It's so incredibly easy you'll wonder why no one else is using it.

gcan




msg:4376017
 4:45 pm on Oct 18, 2011 (gmt 0)

rocknbil, thank you for your answer.

I tried this:

RewriteRule ^([^.]+)$ /profile.php?username=$1 [L,QSA]

Unfortunately, it doesn't work for me - it rewrites existing folders too :(

lucy24




msg:4376177
 8:27 pm on Oct 18, 2011 (gmt 0)

How many existing directories are there? Will anything other than user profiles ever have a query string?

You can either nip them in the bud via a specific RewriteCond ending in

!(realplace|otherrealplace|yetanother)/

or let the php script take them and then kick them right back after checking names. You probably know better than we do which version would place less strain on your system.

g1smd




msg:4376200
 8:58 pm on Oct 18, 2011 (gmt 0)

If you make sure that it doesn't rewrite requests that end in a slash then it will not rewrite folders. So [^.] should perhaps be [^/.] here.

Make sure that all literal periods are escaped. You missed a few.

Slashes do not need to be escaped.

Add the [L] flag to every rule.

gcan




msg:4376501
 11:42 am on Oct 19, 2011 (gmt 0)

Lucy 24,

I have about 40 directories. It's too much?

No, only user profiles will have a query string (except other rewrite rules).

G1smd,

Yes, this works for me:

RewriteRule ^([^/.]+)$ /profile.php?username=$1 [L,QSA]

However, it causes the following problems.

If I call an existing directory without ending slash, for example:

[mydomain...]

it rewrites in this way:

[mydomain...]

And of course [mydomain...]

causes 404 error.

Would like to find some solution so that everything works with and withoud ending slash.

lucy24




msg:4376685
 6:10 pm on Oct 19, 2011 (gmt 0)

Use example.com in all your examples to keep the Forums software from rewriting your links.

Don't know about you, but this is what the rest of us see:
If I call an existing directory without ending slash, for example:

[mydomain...]

it rewrites in this way:

[mydomain...]

And of course [mydomain...]

causes 404 error.


Not very helpful eh ;)

gcan




msg:4376754
 8:42 pm on Oct 19, 2011 (gmt 0)

I am sorry. Forum script parsed links automatically and I can't edit my post. So, I post one more time:

=====================================
Yes, this works for me:

RewriteRule ^([^/.]+)$ /profile.php?username=$1 [L,QSA]

However, it causes the following problems.

If I call an existing directory without ending slash, for example:

http://mydomain/existing-directory

it rewrites in this way:

http://mydomain/existing-directory/?username=existing-directory.

And of course

http://mydomain/username/

(with ending slash) causes 404 error.

g1smd




msg:4376756
 8:50 pm on Oct 19, 2011 (gmt 0)

Use example.com in this forum. See RFC 2606.

lucy24




msg:4376821
 11:11 pm on Oct 19, 2011 (gmt 0)

Now, wait a minute.
If I call an existing directory without ending slash, for example:

http://www.example.com/existing-directory

it rewrites in this way:

http://www.example.com/existing-directory/?username=existing-directory.

That would be wrong anyway, wouldn't it? You're ending up with "directoryname" in two places, and that can't possibly be what you intended.

RewriteRule ^([^/.]+)$ /profile.php?username=$1 [L,QSA]

Where's your RewriteCond that says "only do this if the query string does not already contain 'username=blahblah'"? And what happened to the "profile.php"?

Poring over the sacred writings [httpd.apache.org] tells me that the directory-slash redirect is done by mod_dir, which probably executes after mod_rewrite but before mod_autoindex. (That's assuming reverse alphabetical order. If you are on shared hosting, all of this flies out the window.)

gcan




msg:4377300
 8:49 pm on Oct 20, 2011 (gmt 0)

URL rewriting is like a dark night for me :(
Found a similar post where the same problem happends:

[stackoverflow.com ]

It looks that there are no other alternatives then:

RewriteCond %{REQUEST_FILENAME} !-d

g1smd




msg:4377323
 9:46 pm on Oct 20, 2011 (gmt 0)

It depends what your're doing.

Often
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /somepattern\ HTTP/ is useful.

It ensures the pointer being tested was set as a result of an external request and not from a previous rewrite.

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