cohen
cohen

Reputation: 621

mod rewrite doing something weird

http://localhost/clean_urls/user/whtffgh redirect fine to this http://localhost/clean_urls/user/whtffgh/ (pretty much anything I put there works).

http://localhost/clean_urls/user/cohen however, redirects to this http://localhost/clean/ and I have no idea why. It should just add a /

Heres my .htaccess code

<ifModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_URI} !(.*)/$
    RewriteRule ^(.*)$ /clean_urls/user/$1/ [L,R=301]

    RewriteBase /clean_urls/user/

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d

    RewriteRule ^([a-zA-Z0-9]+)/$ index.php?user=$1


</ifModule>

UPDATE:

The code works besides when I put cohen as the username. The second part of the .htaccess file is making user/index.php?user=username look like user/username so its just a $_GET variable that looks nice in the url and is SEO friendly (apparently).

I am just learning this stuff and making a webapp to try it out so not really sure where its going but I do think I will need another variable on the end. http:://localhost/clean_urls/user/fred/someverb

Upvotes: 0

Views: 44

Answers (1)

TerryE
TerryE

Reputation: 10878

You misunderstand what the RewriteBase directive is used for. It helps the rewrite engine in a "per directory" context to convert the relative URIs to the correct filename equivalent.

You should only have one RewriteBase directive per .htaccess file. The rewrite engine by default will use the lowest .htaccess file on the path with the RewriteEngine On directive set.

So in DOCroot (that is the directory where a "GET /xxx" is mapped to, this should be

RewriteBase /

If your .htaccess file is in DOCroot/clean_urls/user then it should have

RewriteBase /clean_urls/user/

How do you want to decode URIs of the form http:://localhost/fred or are you only wanting do process URIs of the form http:://localhost/clean_urls/user/fred/. What about http:://localhost/clean_urls/user/fred/someverb?

Let me know and where you are putting you .htaccess then I can give you the right content.

Upvotes: 1

Related Questions