Page 1 of 2 12 LastLast
Results 1 to 10 of 15

Thread: mod_rewrite - short urls?

  1. #1
    Join Date
    Jun 2010
    Location
    germany
    Posts
    19
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Question mod_rewrite - short urls?

    Hi there!

    I have many subfolders; lets say 200. And any of them contents a index.html. (all my links are static! NOT dynamic like index.php?id=something here)
    A specific link for this time looks like this:
    Code:
    <a href="folder_1/folder_2/specific_folder/index.html">linktext</a>
    At this point you can see, it's all normal.
    Right. ^^

    Ok, now i don't want this long url in the browser. It looks not good and(!) maybe a hacker or whatever can now check the folders. I just won't it.
    I want to show it like this:
    Code:
    <a href="specific_folder/name-of-content">linktext</a>
    But how i can do this?
    Wordpress can do this and many other boards, too.

    So i have think about it and searched for many weeks(!), but found no solutions.

    Can you help me? Please?

    Because my next Problem ist, the "specific_folder" changes with the content.
    It's not all the same if you know what i mean.

    It is very hard for me to explain this all, because my english is very bad. But the other german forums can't help me. Maybe they're fool or something. ^_^

    With other (short) words:
    Now i have looong urls and i want it short because i could stop spending time for hours and hours to implement long links in to my homepages.

    I hope very very much, anyone can help me further.
    Thank you very much!

    Best Greetings from Northgermany, miow

    EDIT:
    Hmmm, or i have to do dynamic urls like "index.php?id=yadayada" for my solution??
    It's very hard to understand for me.
    Last edited by miow; 08-16-2010 at 02:54 PM.

  2. #2
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    systems like Wordpress and bulletin boards use a database to store their content: they don't necessarily have actual directories and pages. mod-rewrite is used to send info to the system and find the appropriate content.

    Daniel (djr) did a good beginner tutorial on "pretty urls." (make sure you read the whole thing before you start - some things change, and others can be done several ways.)

  3. The Following User Says Thank You to traq For This Useful Post:

    miow (08-17-2010)

  4. #3
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Willkommen bei Dynamic Drive.

    My tutorial will work if you can use php for the main page. (The others can be plain HTML.)

    Otherwise, you can use only htaccess to do this though I'm not sure of the code. You will need to learn regular expressions to create the pattern rule.
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  5. The Following User Says Thank You to djr33 For This Useful Post:

    miow (08-17-2010)

  6. #4
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

  7. The Following User Says Thank You to traq For This Useful Post:

    miow (08-17-2010)

  8. #5
    Join Date
    Jun 2010
    Location
    germany
    Posts
    19
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Default

    Well, i got it!!!!!!!
    Thank you all very very much.
    I have now played with the variables for Hours and now i'm ready.
    My example:
    RewriteRule ^(flash-cartoons)/([A-z0-9_-]+)$ funny_stuff/$1/$2/index.html [R,NC]

    But please explain me:
    Sometimes i see before the end tag "$" a "?" sign. like this:
    ^(flash-cartoons)/([A-z0-9_-]+)?$
    Can you tell me for what that is?
    Thank you very much!

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~
    ...and i got a problem now. ^^

    If i have double content like movie 1 and movie 2, i can't seperate them!
    And if i add 2 different rules for that (to my existing one above this post), mod_rewrite ignore them.
    Like this:
    RewriteRule ^(flash-cartoons)/(anivsani-1)$ dir_1/dir_2/anivsani/anivsani1.html [R,NC]
    RewriteRule ^(flash-cartoons)/(anivsani-2)$ dir_1/dir_2/anivsani/anivsani2.html [R,NC]

    But if i remove the one rule from the post above here, the other 2 rules works fine!

    How i can fix that?

    Thank you very much!
    ~~~~~~~~~~~~~~
    ^ (this post is re-edited) ^
    Last edited by miow; 08-17-2010 at 05:02 PM.

  9. #6
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    the first rule matches, so apache doesn't look at the others. Your rules should go from (most specific) -> (least specific).

  10. The Following User Says Thank You to traq For This Useful Post:

    miow (08-17-2010)

  11. #7
    Join Date
    Jun 2010
    Location
    germany
    Posts
    19
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Default

    Well, i have to say i don't understand really much.
    But i've made it so far. And of course i don't know it is "right" or conform. But how i said it works:
    Code:
    RewriteRule ^(flash-cartoons)/([A-z0-9_-]+)$ dir_1/dir_2/$2/index.html [R,NC]
    RewriteRule ^(flash-cartoons)/(anivsani/2)$ dir_1/dir_2/anivsani/anivsani2.html [R,NC]
    RewriteRule ^(flash-cartoons)/(saladfingers/2)$ dir_1/dir_2/saladfingers/index02.html [R,NC]
    RewriteRule ^(flash-cartoons)/(saladfingers/3)$ dir_1/dir_2/saladfingers/index03.html [R,NC]
    RewriteRule ^(flash-cartoons)/(saladfingers/4)$ dir_1/dir_2/saladfingers/index04.html [R,NC]
    RewriteRule ^(flash-cartoons)/(saladfingers/5)$ dir_1/dir_2/saladfingers/index05.html [R,NC]
    RewriteRule ^(flash-cartoons)/(saladfingers/6)$ dir_1/dir_2/saladfingers/index06.html [R,NC]
    RewriteRule ^(flash-cartoons)/(saladfingers/7)$ dir_1/dir_2/saladfingers/index07.html [R,NC]
    RewriteRule ^(flash-cartoons)/(saladfingers/8)$ dir_1/dir_2/saladfingers/index08.html [R,NC]
    The links are like this for episodes of movies:
    Code:
    http://mydomain.org/flash-cartoons/saladfingers/2
    ...and as a normal link:
    Code:
    http://mydomain.org/flash-cartoons/saladfingers
    Mmmhyeah, for sure it works now. But i'm not sure that's ok.
    - Anyone tips for me or a good advise for me???

    But you all help me muuuuch much more than the other stupid germans in they forums!!!
    This is why i love american forums aaaand of course: Dynamic Drive! (got many FULLworking scripts in my homepage from dynamic drive )

    Greetings from the northern outskirts of germany, miow ^^

  12. #8
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Using regex (regular expressions), you should be able to create a pattern. This will match what you are looking for then you will not need a rule for each name. You can use variables.
    This is not easy, but it is better than writing one line for each page.
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  13. The Following User Says Thank You to djr33 For This Useful Post:

    miow (08-17-2010)

  14. #9
    Join Date
    Jun 2010
    Location
    germany
    Posts
    19
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Default

    Hmm, well..... what?
    To learning this, can you give me a little example?
    Forgive me but this is for me really understandable (or something else^^)

    Thank you!
    Last edited by miow; 08-18-2010 at 08:26 AM. Reason: url removing

  15. #10
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Code:
    RewriteEngine On
    RewriteBase /test
    RewriteCond %{REQUEST_URI} !^/test/index.php$
    RewriteCond %{REQUEST_URI} !^/test/index.php/
    RewriteRule ^(.*)$ index.php/$1?%{QUERY_STRING} [L]
    This is the code from my tutorial. The important part is that line:
    Code:
    RewriteRule ^(.*)$ index.php/$1?%{QUERY_STRING}
    Regular expressions are hard, but this is a basic one:
    (.*) means "anything". The parentheses separate it as a group.
    Then the $1 variable in the output URL is equal to the contents of that.

    /mypage becomes index.php/mypage

    Now this isn't what you want. But the same idea can work.
    You should be able to create variables like that, using () groups. Then use $1, $2, $3, ... and you can make a rule that creates the URLs instead of writing them yourself.

    Does that help?

    I don't understand regex (regular expressions) very well, so this is hard for me also.

    One option would be to hire someone to write this code for you. It should not be very hard for someone who knows regex well, but for the rest of us, it is hard!
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  16. The Following User Says Thank You to djr33 For This Useful Post:

    miow (08-17-2010)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •