View Full Version : 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:
<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:
<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.
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.)
djr33
08-16-2010, 08:39 PM
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.
this (http://www.addedbytes.com/for-beginners/url-rewriting-for-beginners/) might be helpful too.
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) ^
the first rule matches, so apache doesn't look at the others. Your rules should go from (most specific) -> (least specific).
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:
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:
http://mydomain.org/flash-cartoons/saladfingers/2
...and as a normal link:
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!!! :D
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 ^^
djr33
08-17-2010, 05:21 PM
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.
Hmm, well..... what? :confused: :D :D
To learning this, can you give me a little example?
Forgive me but this is for me really understandable (or something else^^)
Thank you! ;)
djr33
08-17-2010, 05:52 PM
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:
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!
I understand a bit and i have a couple of questions. :D
1.) RewriteBase is in my htaccess on "/" and not for a dir. I've read about it and know i can specify some folders.
But how can i have more RewriteBase like this(?):
RewriteBase /
RewriteBase /funny_stuff
RewriteBase /more_dirs
1. a) Or is this for a htaccess per directory? Because i have only one global working htaccess (except sub-domains/wildcards)
2.) {REQUEST_URI}, for what is this?
3.) Why only [L] and not [R]? I thought R is redirect and with NC non case sensitiv.
4.) {QUERY_STRING}, and what for is this? *lol*
I read so much about this but i can't (don't ?) understand what that do, like request_uri.
A few questions for now and if i can understand this, the massive "episodes" rules are history.
I hope! :D
Thank you very much! (again^^)
djr33
08-18-2010, 05:01 AM
1) I believe you only use one rewrite base for each .htaccess file. To create different events, you will use different rules and conditions. The base is only to determine the most basic path from which the rest is determined: If you use a base of "/folder1" then the path "/folder2" will be using "/folder1/folder2". Almost always, it makes sense to use "/" as the base because it is standard. Then you can write anything you want as a path in a rule or a condition.
1a) You can use .htaccess files in different folders, but this is for a different reason: the most local (closest) .htaccess file is the most important. Your main .htaccess file works for the whole site, then any .htaccess file in a folder works only in that folder and in subfolders. You can do this for anything you want.
2) In .htaccess files, the special terms in {} are information on the server. There are a lot of different terms, but REQUEST_URI is the full request URL from the user. If they want your page about Germany, it will be: "/germany/index.html".
3) There are many options. [L] means last rule-- if this matches, it stops here. [R] means redirect, and this happens in the browser. mod_rewrite doesn't do a redirect: it does a hidden "rewrite" so that the browser sees a different page, but it sees it from the same URL it requested. Does that make sense? Redirects and mod_rewrite do very different things, and it's important to know which you want.
4) Same as (2), but the "query string" is the part of the URL after the "?". For example: mypage.php?myvariable=myvalue--- the "query string" is: "myvariable=myvalue".
Thank you very much for this detailed information!!!
So i think in my case it's ok with my rules. Because i use pretty "old" links (oldschool) without php and id's. I think about to change this for many times and everytime i said to: what for?
As you could see, my Homepage is pretty small but with many videos/contents. And i think it is too much work for all if i do like in your tutorial.
Well, yes, i understand now what is meant with request_uri and so on. Like you said before it is only for if i make appropriate links in the .php file.
And thats the point. ;)
I think, after reading at all, i decide to "i let it be right now as is" and well, you could also see, the links in my flash-cartoons section working really good with your help.
I don't know why i can't use stressed (Unterstrich => _ ) in my "first" rule like "flash_cartoons" and i don't know, how i should set a [L] (if neccessary).
But i can live with this. You know how i mean?
I am glad to have made it all possible with your help and mmhyeah, one questione left. ;)
Why i have to use this: ^(flash-cartoons)/([A-z0-9_-]+)$
And why does this not work: ^flash-cartoons/([A-z0-9_-]+)$
And last why: Why i can't use this "stressed" (Unterstrich) like this:
^(flash_cartoons)/([A-z0-9_-]+)$
But anyway, thank you (all) very much and now i have to build in some scripts from Dynamic Drive. ;)
See ya later, miow ^^
djr33
08-18-2010, 04:52 PM
Regular expressions are hard to learn. I am not an expert either. Someone else may be able to explain better.
Here are some guesses about why:
1. The underscore character _ is probably a symbol that has a meaning in regex. Like parentheses (), it may do something to the code. Therefore, it can't be entered as a literal (a character that matches, rather than a character that modifies). Often, the way to work around this is to "escape" the character. This means placing a slash before it. It is often used with quotes, like 'can\'t'. In this case if you use \_ it might work.
2. The parentheses create a group. For some reason this works better. I'm not sure why, but this is probably because within parentheses the letters don't mean anything else. Outside the parentheses, they might. For example "d" means "digit" (number) so you could match any single-character number, like 1, 5, or 0. I'm not sure exactly why this works like that, but I don't think it is a problem-- if it works now, that should be fine.
Ahhh, well ehmmm, i don't need to say i love you or? :D
To the facts:
"Underscore" ( _ ) wasn't right from google-translate. I hate this. ^^ I typed "Unterstrich" in (attention: ) lowercase. The answer was "stressed".
And just now i type Uppercase "Unterstrich"...tadaaaa... (happiness^^) => "Underscore".
What i want to say with this?
I'm learning by doing.
Like this with htaccess, like with php, like with html...and so on.
32 years old how i am, it is my goal, all what i can learn with american (-power ^^) help, to turn it over in my homepage sourcecode.
Yes, it IS hard. :D
But i think you know this. Everybody does know this since he started an own homepage with certain(s ?) personal goals.
Uhh, maybe bad english. I know. ;)
However.
You wrote the one word, i've seeing it at my last javascript snippet included my homepage:
"Escape" the character!
Well, that explains many many things for me. (Include the question about "(flash-cartoons)" and so on.)
Ok, like i said, that explains a lot!!!
Thank you very much!!!!
To the second one, i'm wondering about a bit, because i've never thought sooo far about it. It's very intelligent. Since existing mod_rewrite (i believe anything in the end of the 90's), i've never interested about this and now i realize, it's nearby more powerful then php.
I just say. Because i had years before begin to read about it.
Hope you know what i mean.
So before we come to an end with this "discussion", i have to say thank you. Because (you know about this), you explain me soo much and i swear to god, no one of the other ones in germany could not enlighten(explain) me about that.
You don't know how you helped me soo much.
It's all clear for me and i guess, i have to read more about some apache-tools. ;)
Best wishes and Greetings, your miow
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.