Forum Moderators: phranque

Message Too Old, No Replies

htaccess help redir keep url.

help!

         

doughmain

4:32 pm on Feb 22, 2010 (gmt 0)

10+ Year Member



ive been trying to get this to work with examples from this site, but for what i need. nothing is working.

PLEASE HELP!

i have a subdirectory:
mydomain.com/example/

within this subdirectory i have other subdirectories:
mydomain.com/example/example2/

i want it so when someone goes to:
mydomain.com/example2

it will send them to:
mydomain.com/example/example2


ALSO!;

WHEN someone goes to mydomain.com/example2 I want it to look like that in the URL. I dont want the mydomain.com/example/example2 in the url address bar, i need the url to be what someone enters like mydomain.com/example2 without trailing slash, and if someone adds a slash mydomain.com/example2/ i want it removed.


ALSO - I have other files/folders on my site that dont have anything to do with this /example/ /example2/

im saying this cause when I tried to get this working, the htaccess file would change the urls and redirect image files and such on other parts of my site...

please help! thanks in advance

g1smd

7:01 pm on Feb 22, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



"send" is meaningless - Do you want a rewrite or a redirect? From the longer description a rewrite seems in order.

"Nothing works" is unhelpful - What code have you tried and what was the result?

There's been several posts on this topic in recent days, all with example code you can use.

doughmain

7:55 pm on Feb 22, 2010 (gmt 0)

10+ Year Member



i tried a few different things didnt save the ones i tried... and it doesnt matter what way its done just so it does what my description says... i just want:

mydomain.com/user


and not mydomain.com/example/user/

..

g1smd

8:49 pm on Feb 22, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



OK, but as per the forum charter you need to supply your code to be debugged.

doughmain

8:52 pm on Feb 22, 2010 (gmt 0)

10+ Year Member



RewriteEngine on

RewriteCond $1 !^example
RewriteRule ^(.*) /example/$1 [L]


ok there, now how do i make it like i want in my original post?

thanks in advance

doughmain

11:32 pm on Feb 22, 2010 (gmt 0)

10+ Year Member



It would also be nice if it would take out the www.

so it would be like http://example.com/example2

and not http://www.example.com/example2

[edited by: jdMorgan at 3:27 pm (utc) on Feb 25, 2010]
[edit reason] example [/edit]

jdMorgan

3:11 pm on Feb 23, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Your code seems to do what you stated you wanted it to do. However, it may do additional things that you don't want it to do, or it may not do some things that you also want it to do. However, since your posts are short and lacking in organized detail needed to determine this, it's not possible for us to know.

"Taking out the 'www.'" is referred-to as a domain-canonicalization redirect, and has been covered in many hundreds of previous threads. Please use the site search function to finds many examples of many various methods/rules that you can choose from to add to your configuration. Our Apache Charter also contains links to useful resources, and our Apache Library contains previous threads of particular merit.

Please be aware that everyone here is an un-paid volunteer, just "giving back a little" to the community as their time allows. Therefore, please make the best use of their contributed time. The contributors may be able to clarify sticky points in code or documentation, but may lose interest if asked to do it all for you.

Jim

doughmain

8:08 pm on Feb 23, 2010 (gmt 0)

10+ Year Member



in my first post i specifically said everything that needed to be said, i couldnt imagine what more info you could need:

i have this:
mydomain.com/profiles/user/

/user/ would be the users username


i want this:
mydomain.com/user

/user would be the users username, the folder in /profiles/


i just need a little help, thats what i came here for... but nothing im looking through will do what i need.

please help? or url to where this is already solved

g1smd

8:16 pm on Feb 23, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



How are the current URLs handled? Do they point to real folders inside the server, or does a rewrite fetch content from some other place?

That has a huge bearing on what needs to be done.

If the pattern is 'too wide' it may interfere with URL requests that it should not touch; hence your 'problems' with image and CSS files.

doughmain

9:20 pm on Feb 23, 2010 (gmt 0)

10+ Year Member



it is a simple folder, inside is just a index.php file...

jdMorgan

3:58 am on Feb 24, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Adding a slash to the end of your rule's substitution path and changing "example" to "profiles" in the RewriteCond pattern should do what you said you want. It may do lots of things that you didn't say that you don't want as well.

You may, for example, have a mess with robots.txt, sitemap.xml, images, scripts, and other "common" files, which will now all be treated as "users." If you'd like to avoid that, I would suggest that a better pattern for the rule might be "^([^./]+)$" or "^([a-z0-9_-]+)$", assuming in the first case that slashes or periods are not allowed in usernames, and in the second, that only alphanumeric plus underscore and hyphen are allowed.

The above also assumes that you've got other working rules in that .htaccess file, and so have all the required "set-up" directives for mod_rewrite already taken care of, don't use MultiViews or AcceptPathInfo, etc.

If not, I'd suggest adding

Options +FollowSymLinks -Indexes -MultiViews

above your RewriteEngine directive as a basic starting configuration (which may need to be modified based on your current server configuration).

Jim

doughmain

2:55 pm on Feb 24, 2010 (gmt 0)

10+ Year Member



Thanks Jim.

heres what i got now in my whole .htaccess file

Options +FollowSymLinks -Indexes -MultiViews 
RewriteEngine on

RewriteCond $1 !^profiles
RewriteRule ^([a-z0-9_-]+)$ /profiles/$1/ [L]



it works fine and keeps the url like i want. just 1 thing:

mydomain.com/user - works fine
mydomain.com/user/ - gives a 404

how would i remove the "/"? I would rather it be removed then get it to work.

thanks in advance for your time & help!

g1smd

7:31 pm on Feb 24, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Test for direct client request with path of username/ and send a 301 redirect to www.example.com/username for those requests.

doughmain

5:03 am on Feb 25, 2010 (gmt 0)

10+ Year Member



i dont know how to go about doing that... could you just tell me the code or point me in the right direction.

doughmain

5:12 am on Feb 25, 2010 (gmt 0)

10+ Year Member



also i just found out:

when i said:
http://example.com/user - works fine
http://example.com/user/ - gives a 404

this is still true but when i try www.example.com/user/ or www.example.com/user it redirects to www.example.com/profiles/user

so im guessing i need to add the code to remove www. and ending slashes if someone tries to use them.

[edited by: jdMorgan at 3:26 pm (utc) on Feb 25, 2010]
[edit reason] example.com [/edit]

jdMorgan

3:25 pm on Feb 25, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You just need to add a rule specific to each "problem" case, with redirects first, in order from most-specific to least-specific URL-path, followed by internal rewrites, again in order from most- to least-specific:

Options +FollowSymLinks -Indexes -MultiViews
RewriteEngine on
#
# Externally redirect only direct client requests for /profiles
# URL-paths back to corresponding URL-path in root
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /profiles(/[^\ ]*)?\ HTTP/
RewriteRule ^profiles(/([^/]*))?/?$ http://example.com/$2 [R=301,L]
#
# Externally redirect to remove trailing slash from "user" URL-path requests
# (The requested URL-path is assumed to be a "user" request if it meets the username
# "character-set" requirements and does not resolve to an existing directory)
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([a-z0-9_-]+)/(\?[^\ ]*)?\ HTTP/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z0-9_-]+)/$ http://example.com/$1 [R=301,L]
#
# Externally redirect to force canonical domain name
RewriteCond %{HTTP_HOST} !^(example\.com)?$
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
#
# Internally rewrite "user" requests to /profiles subdirectory
RewriteCond $1 !^profiles
RewriteRule ^([a-z0-9_-]+)$ /profiles/$1 [L]

This does the following:

  • If a client requests "/profiles", "/profiles/", or "/profiles/user1", it gets redirected back to example.com/, example.com/, or example.com/user1 respectively. The /profiles subdirectory-path is no longer accessible as a URL. It exists only as a "file storage area" inside your server, and is only accessible from the Web with URLs such as "example.com/user1". The URL and the filepath are now different from each other.

  • If a client requests "/user1/", it gets redirected to example.com/user1 -- the trailing slash is removed.

  • If a client requests "www.example.com/<whatever>, it gets redirected to example.com/<whatever>

  • If a client requests "/user1", the request is internally rewritten to the filepath /profiles/user1

    Delete your browser cache before testing any new code.

    Jim
  • doughmain

    6:51 pm on Feb 25, 2010 (gmt 0)

    10+ Year Member



    thanks for all the help Jim!

    this was not working at first, i had to put a / after $1 on the last line of code

    RewriteRule ^([a-z0-9_-]+)$ /profiles/$1/ [L]

    without that "/" it would error in IE and firefox, but all seems to be working great now! thanks a lot!

    jdMorgan

    8:29 pm on Feb 25, 2010 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    You've defeated the intent of the second rule then.

    Please describe the structure of the /profiles/ directory and its subdirectories.

    If all profiles are stored as "files" ot "pages" directly under /profiles/, then the inclusion of a trailing slash is a mistake. File and page URLs don't end with slashes, only directory URLs end with slashes.

    So the important question is "Why doesn't it work without a trailing slash that should not be there?" Is there a script under /profiles/ that erroneously requires a trailing slash, or is there further rewrute code in that subdirectory which requires it?

    Jim

    doughmain

    7:33 pm on Feb 26, 2010 (gmt 0)

    10+ Year Member



    within this subdirectory (profiles) i have other subdirectories (different users):

    mydomain.com/profiles/user/
    mydomain.com/profiles/userblah/
    mydomain.com/profiles/anyuser/
    mydomain.com/profiles/jim/

    mydomain.com/profiles/ is just a folder, inside there are only other folders (user folders). inside each user folder is a index.php file.

    even though the users is a folder, and ends with a / i dont want the / shown in my url [mydomain.com...]

    not [mydomain.com...]

    also still dont want www. anywhere -

    thanks for the help, let me know what you think Jim!

    jdMorgan

    8:50 pm on Feb 26, 2010 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    Heading out for awhile, and will consider this. As long as you have subdirectories under /profiles/, the use of the trailing slash is not only acceptable, but required. So, I'll have to review my above code recommendation later. In the meantime, please observe that if you do not use "example.com" in your posts --as requested in our Apache Forum Charter-- then your URLs become unreadable (as above).

    Jim

    jdMorgan

    1:18 am on Feb 27, 2010 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    OK, this made my brain hurt a bit, but on review, I think the modification you made will be OK with the changes shown below. Just be sure to test all variations of URLs like
    example.com/profiles
    example.com/profiles
    example.com/profiles/<user>
    example.com/profiles/<user>
    example.com/<user>
    example.com/<user>/
    and make sure those all do what you want.

    Options +FollowSymLinks -Indexes -MultiViews
    RewriteEngine on
    #
    # Externally redirect only direct client requests for /profiles
    # URL-paths back to corresponding URL-path in root
    RewriteCond %{THE_REQUEST} ^[A-Z]+\ /profiles(/[^\ ]*)?\ HTTP/
    RewriteRule ^profiles(/([^/]*))?/?$ http://example.com/$2 [R=301,L]
    #
    # Externally redirect to remove trailing slash from "/user" URL-path requests
    # (The requested URL-path is assumed to be a "/user" request if it meets the username
    # "character-set" requirements and resolves to an existing subdirectory of /profiles/)
    RewriteCond %{DOCUMENT_ROOT}/profiles/$1/ -d
    RewriteRule ^([a-z0-9_-]+)/$ http://example.com/$1 [R=301,L]
    #
    # Externally redirect to force canonical domain name
    RewriteCond %{HTTP_HOST} !^(example\.com)?$
    RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
    #
    # Internally rewrite "user" requests to /profiles subdirectory
    RewriteCond $1 !^profiles
    RewriteRule ^([a-z0-9_-]+)$ /profiles/$1/ [L]

    Jim

    doughmain

    6:58 am on Feb 27, 2010 (gmt 0)

    10+ Year Member



    all seems good! thanks a lot!

    doughmain

    4:14 pm on Mar 3, 2010 (gmt 0)

    10+ Year Member



    just wondering, how hard would it be to make it so different cases work:

    example.com/UsernaMe
    =
    example.com/username

    jdMorgan

    5:57 pm on Mar 3, 2010 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    Simple in code, more trouble than it is worth in practice. Apache is case-sensitive, and if allowed, users will "forget" how they cased their usernames and won't be able to access their profile directory... When signing up a new user, force all-lowercase and be done with the problem at the start.

    Where [a-z} appears in the patterns in the code above, add "NC" to the flags at the end of that RewriteRule or RewriteCond, e.g. [R=301,L] becomes [NC,R=301,L]

    Jim

    g1smd

    7:59 pm on Mar 3, 2010 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



    But do look at the mess that has caused to Twitter page indexing, where a mixture of /UserName /username /USERNAME /UsErNaMe /uSeRnAmE /USERname /userNAME and many more, all work.

    doughmain

    8:16 pm on Mar 3, 2010 (gmt 0)

    10+ Year Member



    thank you very much for the feedback! i love this site