Welcome to WebmasterWorld Guest from 54.221.165.157

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

Is my .htaccess file OK?

rewrite question

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

Junior Member

5+ Year Member

joined:Mar 20, 2008
posts:116
votes: 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.
4:29 pm on Oct 18, 2011 (gmt 0)

Senior Member

WebmasterWorld Senior Member rocknbil is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Nov 28, 2004
posts:7999
votes: 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.
4:45 pm on Oct 18, 2011 (gmt 0)

Junior Member

5+ Year Member

joined:Mar 20, 2008
posts:116
votes: 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 :(
8:27 pm on Oct 18, 2011 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:13000
votes: 289


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.
8:58 pm on Oct 18, 2011 (gmt 0)

Senior Member

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:July 3, 2002
posts:18903
votes: 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.
11:42 am on Oct 19, 2011 (gmt 0)

Junior Member

5+ Year Member

joined:Mar 20, 2008
posts:116
votes: 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.
6:10 pm on Oct 19, 2011 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:13000
votes: 289


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 ;)
8:42 pm on Oct 19, 2011 (gmt 0)

Junior Member

5+ Year Member

joined:Mar 20, 2008
posts:116
votes: 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.
8:50 pm on Oct 19, 2011 (gmt 0)

Senior Member

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:July 3, 2002
posts:18903
votes: 0


Use example.com in this forum. See RFC 2606.
11:11 pm on Oct 19, 2011 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:13000
votes: 289


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.)
8:49 pm on Oct 20, 2011 (gmt 0)

Junior Member

5+ Year Member

joined:Mar 20, 2008
posts:116
votes: 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
9:46 pm on Oct 20, 2011 (gmt 0)

Senior Member

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:July 3, 2002
posts:18903
votes: 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.
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members