Hey all,

I have the following code in my .htaccess file for my website.

RewriteEngine on
RewriteBase /
RewriteRule ^([A-Za-z_0-9-]+)$ redirect.php?user=$1
RewriteRule ^([A-Za-z_0-9-]+)/$ redirect.php?user=$1 

Now let me explain. I basically host a blog site for a few friends of mine and I make it possible for users to view their blog by visiting www.website.com/username instead of having to go to www.website.com/redirect.php?user=username.

The only problem with this is if somebody types in 'www.website.com/username/' instead of 'www.website.com/username' they will get a 404 error. How should I modify the .htaccess code to alleviate the problem with the extra slash at the end?

I would think that this would work...but the .htaccess files on my server are odd in a sense. I had the feeling it wasn't making a difference, so I removed it using PuTTY ('ls -al' to view the hidden file) and it still redirects correctly (without the ending slash) after the .htaccess is deleted!

What is going on?

thanks, -influx

    that looks like summin ud also need to edit in php.ini dont quote me lol im taking a guess

      Funny, I've answered this same exact question before.

      Did you search the boards?

      Try this out, as mentioned in this thread:

      Originally posted by bradgrafelman
      Try this:

      RewriteEngine on
      RewriteBase /
      RewriteRule ^([A-Za-z_0-9-]+)((/)|())$ user_home.php?user=$1

      [/B]

      Just modify the user_home to the appropriate script name.

      EDIT: You, er... removed the htaccess and it still redirected? Erm, try doing cat .htaccess in the shell (make sure you're in the correct directory). What happens?

      EDIT2: Just tried that RewriteRule (once again) on my own server - worked flawlessly.

      EDIT3: The only way I can restore a normal .htaccess file on a friend of mine's website (his webhost did not provide shell access, and the FTP server/client, naturally, hide's files prefixed with a period) is to make a file called .htaccess on my comp that has nothing in it - a blank file. Then, I upload that to the directory that had the 'old' htaccess file. Whether it asks you to overwrite or not, it should replace your htaccess with a blank file, therefore resetting all of Apache's default configurations.

        that previous thread was mine as well =) I tried inquiring for more help, but to no regard. Hm, I do suspect it should work but it just doesn't respond!

        i'll try the cat .htaccess trick and tell you exactly what it tells me in the next reply.

          The 'cat .htaccess' just displays what is in the .htaccess file...exactly what was listed above.

          I tried TIP #3, uploaded a blank htaccess, and it still redirected correctly.

          I also have something else listed below those commands, could that be affecting it?

          Here is the full .htaccess:

          RewriteEngine on
          RewriteBase /
          RewriteRule ([A-Za-z_0-9-]+)((/)|())$ redirect.php?user=$1

          ErrorDocument 400 http://www.website.com/error.php?c=400
          ErrorDocument 403 http://www.website.com/error.php?c=403
          ErrorDocument 404 http://www.website.com/error.php?c=404
          ErrorDocument 500 http://www.website.com/error.php?c=500
          ErrorDocument 503 http://www.website.com/error.php?c=503

            Why two ending slashes?

            The RewriteRule I posted above will handle an ending slash, though I'm confused as to why there would be more than one...

              If your still having a problem, why not use:

              RewriteRule ^([A-Za-z0-9-_])+([/])?$ redirect.php?user=$1

              ?

                Hm, that doesn't seem to be working.

                I have the feeling that everytime I update and upload my .htaccess, it does nothing.

                Even when I remove my .htaccess from the base directory, it still tends to affect my website (it still mod_rewrites the way it did before).

                What could be doing this?

                Could it be the server that is flawed...how could I fix this?

                thanks.

                  I'd like to know the answer to the file caching problem too. Rarely, at random, that happens to me on my server. I usually have to delete the file, go to the file and get a 404 error, then re-upload the file to get it to work.

                  As for the REwriteRule I provided, you might want to take out the [] around the last forward slash. I doubt it will help, but it's worth a shot.

                    RewriteRule ^([-A-Za-z0-9_]+)[/]?$ redirect.php?user=$1

                    That's a better formed regex.

                      Write a Reply...