Forum Moderators: phranque

Message Too Old, No Replies

.htaccess another problem

         

luka

2:56 pm on Dec 15, 2003 (gmt 0)

10+ Year Member



Hello

I`m new here. This is my first post.

I`ve got http://www.example.pl/polish/index.php?sub=main&id=12

Is possible to change this to http://www.example.pl/polish/main/12

or http://www.example.pl/main/12

What i must write in to my .htaccess file?

Sorry for my bad english :)

[edited by: jdMorgan at 6:13 pm (utc) on Dec. 15, 2003]
[edit reason] De-linked, examplified [/edit]

luka

3:38 pm on Dec 15, 2003 (gmt 0)

10+ Year Member



I,ve got something like this

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/polish/index.php
RewriteRule ^\?sub=main\&id=$(.*)$ http://www.example.pl/polish/main/$1.php

but it doesn`t work,

Do you have any ideas?

Please help,

Thanks a lot

Luk

[edited by: jdMorgan at 6:13 pm (utc) on Dec. 15, 2003]
[edit reason] De-linked, examplified [/edit]

ProFiler

4:23 pm on Dec 15, 2003 (gmt 0)

10+ Year Member



This should work:

RewriteEngine on
RewriteRule ^main/([0-9]*) /polish/index.php?sub=main&id=$1

This will rewrite
http://www.example.pl/main/123456
to
http://www.example.pl/polish/index.php?sub=main&id=123456

Not 100% on this but if you use the above line do make sure you put it in a .htaccess file that is in your "main" folder so not in directory polish but itīs parent dir, most likely public_html or www

Anyway, I just needed the above myself today, but after finding out how to do it and getting it working, I ran into a new problem.

Within my page or "set-up" which is:
http://www.example.com/files/21323/ (or without the trailing slash)
being rewritten to
http://www.example.com/file.info?ID=21323

Using this line:
RewriteRule ^files/([0-9]*) file.info?ID=$1

But now to my next problem (the above works fine so thats not it), I also have an option to expand comments on the file, it's url is then

http://www.example.com/file.info?ID=21323&action=expand
(and followed by a #1234 so it "auto-scrolls" down to the comment they want to expand, but that aside)

So what should I rewrite my rule to so that, for example, I can make the expand links:
http://www.example.com/files/21323/expand#1234
(with 1234 being the Comment number to autoscroll to and 21323 being the File ID)

I know you cant do if then statements in apache :) but in pseudo it would be:

if requested url contains "expand" then follow this rule:
RewriteRule ^files/([0-9]*)/expand#([0-9]*) file.info?ID=$1&action=expand#$2

but if it contains no expand then use the rule i already have:

RewriteRule ^files/([0-9]*) file.info?ID=$1

So anyway I have no clue how to handle the above problem, if anyone can shed some light on that issue, thatīd be awesome, thanks in advance!

[edited by: jdMorgan at 6:18 pm (utc) on Dec. 15, 2003]
[edit reason] De-linked, examplified [/edit]

jdMorgan

6:22 pm on Dec 15, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



ProFiler,

Simply add an [L] flag onto the end of your first rule. If the pattern in the rule matches and the URL is rewritten, then mod_rewrite will quit right there and serve the requested resource from the new URL. If not, it will continue on to the next rule.

In most applications, almost *all* RewriteRules should have an [L] flag.


RewriteRule ^files/([0-9]*)/expand#([0-9]*) file.info?ID=$1&action=expand#$2 [b][L][/b]
RewriteRule ^files/([0-9]*) file.info?ID=$1 [L]

Jim

jdMorgan

6:27 pm on Dec 15, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Luka,

Welcome to WebmasterWorld [webmasterworld.com]!

Here is some more recent discussion of dynamic-to-static URL conversion issues: [webmasterworld.com...]

Jim

ProFiler

9:14 pm on Dec 15, 2003 (gmt 0)

10+ Year Member



Thanks for the help Jim however it still doesnīt seem to work:

RewriteEngine on
RewriteRule ^files/([0-9]*)/expand#([0-9]*) file.info?ID=$1&action=expand#$2 [L]
RewriteRule ^files/([0-9]*) file.info?ID=$1 [L]

However, Iīll fix it with PHP and use

http://www.example.com/files/21323
and
http://www.example.com/files/21323expand#1234
(note no slash before expand)
If PHP finds "expand" in the Query ID it will define $action="expand";
And then strip the word from the ID and check if itīs still a valid number.
That means Iīll have to modify the rewriterule regex but that shouldnīt be a problem so it becomes:

RewriteRule ^files/([0-9expand]*) file.info?ID=$1 [L]

And that works (with the nessesary modifications to my php script) I even like it better like this
http://www.example.com/files/21323x#1234

Anyway thx Jim!

[edited by: jdMorgan at 10:16 pm (utc) on Dec. 15, 2003]
[edit reason] Delinked, examplified URLs [/edit]

luka

10:02 pm on Dec 15, 2003 (gmt 0)

10+ Year Member



Jim that's very important for me. Could you please write the whole rule for me for http://www.example.pl/polish/index.php?sub=main&id=12? I will be very grateful. I'm just the beginner.

[edited by: jdMorgan at 10:17 pm (utc) on Dec. 15, 2003]
[edit reason] Delinked [/edit]

jdMorgan

10:23 pm on Dec 15, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



luka,

In accordance with our charter [webmasterworld.com], we will be happy to help you learn to write this code for yourself, and to help you debug it. However, if you need an immediate solution, you might consider posting in our Commercial Exchange [webmasterworld.com] to hire someone to write it for you.

The following post and the links it contains may be useful to you: Introduction to mod_rewrite [webmasterworld.com]

Jim

luka

9:10 am on Dec 16, 2003 (gmt 0)

10+ Year Member



Ok, i`ve read atricles about regular extensions.

I`ve go something like this:

Options +FollowSymLinks
RewriteEngine on
RewriteRule ^/polish/index.php?sub=main&id=(.)$ /main/$1.php [L]

but it doesn`t work :(

Please help me change part of url http:.../polish/index.php?sub=main&id=12

to more friendly url http:...../polish/main/(integer).php

I`ve check many rules :(

It is very important for me:(

Please help me.

jdMorgan

4:28 pm on Dec 16, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Luka,

In order to back-reference the query string, you'll need to use RewriteCond:


Options +FollowSymLinks
RewriteEngine on
RewriteCond %{QUERY_STRING} ^id=(.*)$
RewriteRule ^polish/index\.php$ /main/%1.php [L]

I'd like to re-emphasize something in the thread I cited above: It is the normal case to use mod_rewrite to translate from a static URL to a dynamic URL - the opposite of what you are asking for here - in order to make search-engine-friendly URLs.

It works like this:
The script outputs friendly URLs to browsers and search engine spiders.
The browser or search engine spider then requests friendly URLs.
Mod_rewrite converts friendly-URL requests to requests for your script with a query string.
The script runs, and outputs more friendly URLs, which again may be requested by the browser or spider.

It is important to note *when* mod_rewrite acts; after an HTTP request is received, but before any content is served. For this reason, the usual mod_rewrite action is to translate from friendly to "unfriendly" URLs, not the other way around.

You may indeed have a reason to do the opposite translation - I just want to make sure that the issue is clear.

Jim

luka

7:03 pm on Dec 16, 2003 (gmt 0)

10+ Year Member



Jim you are great! It is realy works!

I`m so happy, Thank you very much!

But I`ve got only one questione. Is possible change http:.../polish/main35.php to http:.../main/35.php without changing directory? I`ve tried to remove "polish" from RewriteRule ^polish/index\.php$ /main/%1.php [L] but it doesn`t work.

jdMorgan

7:09 pm on Dec 16, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Luka,

If I understand your question, you'll need to change the rule:


RewriteRule ^polish/index\.php$ /polish/main/%1.php [L]

Jim

luka

7:27 pm on Dec 16, 2003 (gmt 0)

10+ Year Member



Jim, it`s my foult

Change this dynamic url http:.../polish/index.php?sub=main&id=12 to http:.../main/12.php

Now i`ve got http:.../polish/main/12.php it works grea but is possible to change for http:.../main/12.php?

Just remove "polish" phrase

I must learn english a lot.

Sorry for my mistakes

jdMorgan

7:48 pm on Dec 16, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Luka,

http:.../polish/index.php?sub=main&id=12 -> http:.../main/12.php

would be:


Options +FollowSymLinks
RewriteEngine on
RewriteCond %{QUERY_STRING} id=([^&]*)
RewriteRule ^polish/index\.php$ /main/%1.php [L]

Your english is a lot better than my polish! :)

Jim

luka

8:32 pm on Dec 16, 2003 (gmt 0)

10+ Year Member



Jim, you kill me:)

Finally i`ve decide that my url will be like this

http:.../polish/35.php not http:.../main/35.php

Is possible to change it? I`ve tried but ... you now :)

I will teache you speak in polish. It`s easy to learn, realy.

Dziekuje bardzo == Thank you very much

jdMorgan

9:09 pm on Dec 16, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Just replace "main" in the right side of this rule with "polish":

RewriteRule ^polish/index\.php$ /[b]main[/b]/%1.php [L]

I encourage you to experiment with this - It will help you to learn.

Jim

luka

9:46 pm on Dec 16, 2003 (gmt 0)

10+ Year Member


Jim, when i type http://www.example.pl/polish/35.php for rule

RewriteEngine on
RewriteCond %{QUERY_STRING} id=([^&]*)
RewriteRule ^polish/index\.php$ /polish/%1.php [L]

i`ve got error message 404 file not found.

Why? Where is a mistake?