Forum Moderators: phranque

Message Too Old, No Replies

Static urls

mod_rewrite() module of Apache

         

anshul

8:04 am on Apr 5, 2005 (gmt 0)

10+ Year Member



I've used blogs: WordPress, b2evolution on Apache. These use permalinks/static urls. I guess they fetch urls from database and write rewrite rules into .htaccess files using PHP.

My request from the experts here, is to show few examples to make urls static using .htaccess file.?Once, we've the key concepts, we can manipulte .htaccess file using PHP, to make our Web pages, SE friendly. This is exactly, the blogs, I've mentioned do.

Apache documentation for mod_rewrite() is dry, and I'll be very glad, if you provide me few examples here.( I'll test myself )

Please provide few nice urls for learning regular expressions and Apache mod_rewrite() module functions.

Special thanks.

jdMorgan

12:51 pm on Apr 5, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



For info on using friendly URLs, see this recent thread [webmasterworld.com]. There are a few examples in that thread, as well.

A search on WebmasterWorld for "search friendly URLs [google.com]" and related phrases should turn up hundreds more.

Jim

anshul

1:31 pm on Apr 6, 2005 (gmt 0)

10+ Year Member



Already spent 9-10 hours on it; installed Apache on localhost and worked few .htaccess examples.

When it came to RewriteCond and RewriteRule, I failed invariably! Hence I seek benevolence, please take a time/ease to help in place, here!

I wanna convert:
[localhost...]
[localhost...]
to static urls in browser-bar like:
[localhost...]
[localhost...]
or
[localhost...]
[localhost...]

else I've to pull my hairs for next 9-10 or more hours myself, next-day :¦

jdMorgan

1:37 pm on Apr 6, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



See this recent thread [webmasterworld.com] for explanation and examples.

Jim

jd01

7:32 am on Apr 12, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



anshul,

It looks like you are going backward. IE Your are trying to rewrite a php URL to static. It has to be the other way around. The link on your page has to be to the static url you want to use (fake), then use mod_rewrite to call the php file (real).

Like this:
RewriteEngine ON
RewriteRule ^test/[a-z]/[a-z]/$ /test/index?$1=$2 [QSA,L]

Here is how it works:
The first path (between the ^ and $) is the url you are rewriting (the fake one a user just clicked on). Your .htaccess has to be in a place where it will 'catch' the request correctly. EG If your .htaccess is in your root (www, etc.), you will use the path above... If your .htaccess is in the folder /test/ you would only use ^[a-z]/[a-z]/$

The second path is the full path from your root to the real file you are accessing, so even if the 'real' file is in the test/ folder, you need the full relative URL.

[a-z] catches any lowercase letters and stores them in a variable. (You can use (.*) to catch and pass everything between the slashes.) $1 is the value stored in the first [a-z], $2 the second. $1 and $2 must be the exact variable and value you are passing to your php.

[QSA,L] passes the information as a query string, so it will be recognized by your php file and tells Apache it is the last Rule.

Hope this helps.

Justin

anshul

10:42 am on Apr 12, 2005 (gmt 0)

10+ Year Member



Thank you and you were right ( I was thinking reverse! ) Yes, it is now working. I've also read ( in WordPress blogs' .htaccess )

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [S=35]

Where S is 'skip-flag' and S can be 1, 2, ..35 or more i.e., number of rewrite rules to apply i.e., number of your fake urls.

RewriteRule ^.*$ - [S=35]
is supposed to macth e.g.,
http://www.name.com/
and apply JUST ONLY NEXT ONE rule, if it matches. If next rule don't match, apply next and STOP if it matches. This is good.

Also I learnt, fake urls ( and folders ) should be hardcoded into html, so that browser display will complete urls including [name.com...]
So, we can't use relative-links so!

Thank you for your consideration ( and understandable reply ); I look to learn Apache flags and regular expressions now; yes! these are difficult, if we've to deal complexity ( somehow ). For this, I've just an interesting question in my mind, can we share a folder/sub-domain for two/three different domain names? ( no redirect please! ). After grabbing domain name from where it is referred, we can present it with different style-sheets ( and apply other customs' )!

Also, I saw, PHP can also convert fake folders to query-string; by analysing the url; though mod_rewrite is consise, and far straight-forward.

Lastly, sorry, If my message is off-topic. I am ( continously) thinking! Hope, this post is useful for some visitors, here.

jd01

12:07 pm on Apr 12, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



anshul,

Glad I could help.

You can actually use relative URL's (fake), you just have to make sure the path to the php (real) file is complete.

Your HTML can be linked to widgets/index.html, because the browser puts this together and requests [home...]

But the path in the .htaccess has to route to the full path of the real file including any variables, EG /home/widgets/index.php=?stuff=otherstuff