.htaccess file sort of working but causing problems


I have an .htaccess file on my server which works to a point.

The part to re-write the URL to always include the www works perfectly, and the part to make nice URLs also works in some situations, but has the following problem: (I have read through the Data Koncepts links however I’m too simple to understand what’s going on).

The problem is 2 things.
1: trailing slashes (or the lack of them) still causes problems despite trying many things.
2: Page names that start the same seem to cause problems. So if I have a shortened URL of domain.com/home and another of domain.com/homes then all requests for domain.com/homes go to domain.com/home

My .htaccess file is:

RewriteEngine On

RewriteCond %{HTTP_HOST} !^www\\.domain\\.com [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]

RewriteRule ^home/?([0-9a-zA-Z_]*)(/([0-9a-zA-Z_]*)(/([0-9a-zA-Z_]*)(/([0-9a-zA-Z_]*)(/([0-9a-zA-Z_]*)(/([0-9a-zA-Z_]*)(/([0-9a-zA-Z_]*)?)?)?)?)?)?)?$ home.php?id=$1&name=$3&var3=$5

RewriteRule ^homes/?([0-9a-zA-Z_]*)(/([0-9a-zA-Z_]*)(/([0-9a-zA-Z_]*)(/([0-9a-zA-Z_]*)(/([0-9a-zA-Z_]*)(/([0-9a-zA-Z_]*)(/([0-9a-zA-Z_]*)?)?)?)?)?)?)?$ homes.php?id=$1&name=$3&var3=$5

I’d really appreciate some help as it’s driving me a bit nuts.

Hi grandad!

Okay, if you weren’t able to wade through the regex section of the tutorial, PLEASE ask questions here. At least you know where to find me.

The problem with your regex is the INAPPROPRIATE use of the :kaioken: EVERYTHING :kaioken: atom - especially that it comes before other regex so there is already a redirection before the more specific things are handled!

# OMG! :rolleyes2:
RewriteEngine On

# force www'd domain
RewriteCond %{HTTP_HOST} !^www\\.domain\\.com [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]

# WTF?
# why is the / after home optional?
# Why ZERO or more letters, digits and _'s before the next / - at every point in the URI?
# - Will home.php really accept home//whatever ... in the URI?
# I'll assume the atoms were properly created with balanced () pairs
# NO Last flag!  That portends problems with all the following mod_rewrite code!
RewriteRule ^home[COLOR="Red"]/?[/COLOR]([0-9a-zA-Z_][COLOR="Red"]*[/COLOR])(/([0-9a-zA-Z_][COLOR="Red"]*[/COLOR])(/([0-9a-zA-Z_][COLOR="Red"]*[/COLOR])(/([0-9a-zA-Z_][COLOR="Red"]*[/COLOR])(/([0-9a-zA-Z_][COLOR="Red"]*[/COLOR])(/([0-9a-zA-Z_][COLOR="Red"]*[/COLOR])(/([0-9a-zA-Z_][COLOR="Red"]*[/COLOR])?)?)?)?)?)?)?$ home.php?id=$1&name=$3&var3=$5

# Ditto all of the above
RewriteRule ^homes/?([0-9a-zA-Z_]*)(/([0-9a-zA-Z_]*)(/([0-9a-zA-Z_]*)(/([0-9a-zA-Z_]*)(/([0-9a-zA-Z_]*)(/([0-9a-zA-Z_]*)(/([0-9a-zA-Z_]*)?)?)?)?)?)?)?$ homes.php?id=$1&name=$3&var3=$5

Not really. Trailing slashes WILL cause directory level problems for relative links but nothing more if the TRAILING / is made optional.

Then, “start the same” is irrelevant if they are different before the end of the {REQUEST_URI} string (that’s marked in your regex with the $).

Finally, please communicate IN WORDS what you’re trying to do with your regex (it baffles me) and I’ll be more than happy to help you through that logic (it appears that there is far too much which is optional, i.e., zero or one as denoted by the ?'s).



Thanks for the reply! Sure, I’ll try to explain exactly what should happen.

Firstly, all traffic without the www (so http://domain.com/whatever) should be re-directed to the same URL with the www (so http://www.domain.com/whatever). This works so unless there is something technically wrong that could cause other problems I’m happy with it.

It should also allow me to get variables from URLs without using the variable name in the URL. So for example:


should be able to be written as:

http://www.domain.com/pagename/123/9578 or http://www.domain.com/pagename/123/9578/ (second one includes trailing /)

Everything after the http://www.domain.com/pagename should be optional (including the trailing /), as if there are no variables I have a default set of values. So http://www.domain.com/pagename should just show the page at http://www.domain.com/pagename.php and http://www.domain.com/pagename/123 should just show the page at http://www.domain.com/pagename.php?id=123

Does this make sense?

Cheers :slight_smile:

Sorry, just to add,

http://www.domain.com/pagename//9578/ (second value empty) can also be valid as not all variables are needed - there is always a default built into the script if any are left empty…


Okay, let me look at this (not your previous code) and see what I can do:

RewriteEngine on

# force www
RewriteCond %{HTTP_HOST} !www\\.example\\.com [NC]
RewriteRule .? http://www.example.com%{REQUEST_URI} [R=301,L]

If not www’d version of your domain name, redirect the request to the www’d domain. Note, please, that I’m not worried about matching/capturing anything as it’s already in Apache’s {REQUEST_URI} variable.

Next, look for pagename (Any? Is this a variable? Extensionless?) followed by a / and two sets of digits (or letters) separated by /'s with a possible trailing / (BAD design - that means pagename won’t know what directory level its relative links will be found at). Moreover, VERY bad to have // together in the URI (although Apache shouldn’t choke on that after being processed by mod_rewrite).

RewriteRule ^pagename/(/d+)(/(/d+)?/?$ pagename.php?id=$1&user=$3 [L]

# OR, in more generic terms such as those you used:

RewriteRule ^([a-z]+)/([0-9]*)(/([a-zA-Z0-9_]+))?/?$ $1.php?id=$2&user=$4 [L]

Where the pagename is a string of lowercase letters,

where id could be ZERO or more digits (it SHOULD be one or more, i.e., + rather than *) and

where user is any letter, digit or underscore.

Yeah, yeah, I know you said that already! Simple when you can specify what you’re trying to do, isn’t it?



Thanks for your in-depth reply! You gave me the code to do exactly what I was hoping for, however I am getting an error message…

My code is:

RewriteEngine On

RewriteCond %{HTTP_HOST} !www\\.domain\\.com [NC]
RewriteRule .? http://www.domain.com%{REQUEST_URI} [R=301,L]

RewriteRule ^test/(/d+)(/(/d+)?/?$ test.php?id=$1&user=$3 [L]

I have copied that directly from my .htaccess, the only thing I have changed at all is the domain name.

Error message is:

Internal Server Error

The server encountered an internal error or misconfiguration and was unable to complete your request.

Please contact the server administrator, webmaster@domain.com and inform them of the time the error occurred, and anything you might have done that may have caused the error.

More information about this error may be available in the server error log.

Additionally, a 500 Internal Server Error error was encountered while trying to use an ErrorDocument to handle the request.

If I only use the part for adding the www’s then it works, the error message only appears when the line dealing with test.php is added.

Where am I going wrong?

Thanks again :slight_smile:

I wonder if we’re missing a closing parenthesis in this line:
RewriteRule ^pagename/(/d+)(/(/d+)?/?$ pagename.php?id=$1&user=$3 [L]
For atom #2? If I’m reading it right, it would go right before the last ? to make the / optional.


More information about this error may be available in the server error log.

I dunno if you have access to this, but if you do, it’s awesome, because even if you can’t read it, someone can. Rewrite itself has a log file and it’ll say if something didn’t match, for instance (sometimes, I have trouble reading it lawlz).

Naw, naw, naw! COUNT the open parentheses and the close parentheses. When the count does not match, you WILL get an error from the server (effectively shutting down your site). Good advert for a localhost test server, eh?

RewriteEngine On

RewriteCond %{HTTP_HOST} !www\\.domain\\.com [NC]
RewriteRule .? http://www.domain.com%{REQUEST_URI} [R=301,L]

RewriteRule ^test/(/d+)(/(/d+)[size=+4][COLOR="Red"])[/COLOR][/size]?/?$ test.php?id=$1&user=$3 [L]



mod rewrite impelementation in localhost could be more difficult than in web hosting :smiley:

On what basis? Silly statement (IMHO) so please back it up!