Welcome to WebmasterWorld Guest from 3.94.202.88

Forum Moderators: Ocean10000 & phranque

Message Too Old, No Replies

Rewrite with space

Rewrite with space

     
9:30 am on Nov 20, 2003 (gmt 0)

Junior Member

10+ Year Member

joined:Nov 20, 2003
posts:144
votes: 0


Hi

I have this small problem

I have this link :

page.php?m=Aston Martin

Now I must rewrite

I can use simply this

RewriteRule ^([^/]+).html page.php?m=$1 [l]

But the link view is : Aston%20Martin.html

I prefer to transform in : Aston_Martin.html

Can help me?

Thks

3:43 am on Nov 21, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 31, 2002
posts:25430
votes: 0


webstyler,

Welcome to WebmasterWorld [webmasterworld.com]!

I'm not sure whether you are asking for a specific solution for rewriting "Astin%20Martin" to "Astin_Martin", or for a more general case of rewriting "<anything>%20<anything>" to "<anything>_<anything>", but this thread [webmasterworld.com] discusses several methods for replacing certain characters with others for the general case. It may serve to give you an idea of how to solve your problem.

Jim

7:39 am on Nov 21, 2003 (gmt 0)

Junior Member

10+ Year Member

joined:Nov 20, 2003
posts:144
votes: 0


yes

I must simple tranform Astin%20Martin" to "Astin_Martin"

thks for thread but all the code have more $ for split var

I have only one , $1

RewriteRule ^([^/]+).html page.php?m=$1 [l]

thks

8:07 am on Nov 28, 2003 (gmt 0)

Junior Member

10+ Year Member

joined:Nov 20, 2003
posts:144
votes: 0


? nobody
8:19 am on Nov 28, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 31, 2002
posts:25430
votes: 0


Sure, just use both rules:

RewriteRule ^Astin\ Martin\.html page.php?m=Astin_Martin [L]
RewriteRule ^([^/]+)\.html page.php?m=$1 [L]

Or, a more general solution:

RewriteRule ^([^\ ])\ ([^\ ])$ $1_$2
RewriteRule ^([^/]+)\.html page.php?m=$1 [L]

Note that there is *no* [L] flag on the first rule.
Note also that literal periods "." in regular expressions patterns must be escaped by preceding them with a backslash "\", for example, "file\.html".

Jim

9:07 am on Nov 28, 2003 (gmt 0)

Junior Member

10+ Year Member

joined:Nov 20, 2003
posts:144
votes: 0


thks :))

but

the var that I must send is with space

I need make the inverse operation

I have a link :

page.php?m=Aston Martin

I must, with mod_rewrite, to transform in

Aston_Martin.html
_________
In your code the page called Aston Martin.html is
transform to Aston_Martin.html, and value var is Aston_Martin
_________
I needed that Aston_Martin.html called a page with value var Aston Martin

Thks :))

6:34 pm on Nov 28, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 31, 2002
posts:25430
votes: 0


In Msg#3 above:
> I must simple tranform Astin%20Martin" to "Astin_Martin"

I don't think it's possible to output a space from mod_rewrite. The best you can do is to output a hex-code-escaped space (%20).

A better alternative would be to change your PHP script to recognize a different character (other than a space).


RewriteRule ^Astin_Martin\.html$ page.php?m=Astin\%20Martin [NE,L]
RewriteRule ^(.+)\.html$ page.php?m=$1 [L]

Or, the more general solution:

RewriteRule ^([^_]+)_(.*)$ $1\%20$2 [NE]
RewriteRule ^(.+)\.html$ page.php?m=$1 [NE,L]

Note that there is *no* [L] flag on the first rule.

Note also that literal periods "." in regular expressions patterns must be escaped by preceding them with a backslash "\", for example, "file\.html".

Jim

7:34 am on Nov 29, 2003 (gmt 0)

Junior Member

10+ Year Member

joined:Nov 20, 2003
posts:144
votes: 0


ok :))

now

this is PERFECT
RewriteRule ^Astin_Martin\.html$ page.php?m=Astin\%20Martin [NE,L]

but this return error 404
RewriteRule ^([^_]+)_(.*)$ $1\%20$2 [NE]

?

thks for your patience ;)

5:32 pm on Nov 29, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 31, 2002
posts:25430
votes: 0


but this return error 404
RewriteRule ^([^_]+)_(.*)$ $1\%20$2 [NE]

OK, I'm still not clear on what you are trying to do. Assuming that you request Astin_Martin.html, that RewriteRule will require that you have a file named "Astin Martin.html" on the server. Apache won't support that for a filename, but can support it for a call to a script. If you still want to call your script, you'll have to include the script path in the rewrite. So for the general case of redirecting requests for <anything>_<anythingelse> to /page.php?m=<anything><space><anythingelse>, you'd want:

RewriteRule ^([^_]+)_(.*)$ /page.php?m=$1\%20$2 [NE]

Jim
8:19 am on Nov 30, 2003 (gmt 0)

Junior Member

10+ Year Member

joined:Nov 20, 2003
posts:144
votes: 0


:(

RewriteRule ^([^_]+)_(.*)$ /page.php?m=$1\%20$2 [NE]

this make a loop, and I must restart Apache?

thks

4:53 pm on Nov 30, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 31, 2002
posts:25430
votes: 0


> this make a loop, and I must restart Apache?
> RewriteRule ^([^_]+)_(.*)$ /page.php?m=$1\%20$2 [NE]

Is it possible that the name of your php page actually contains an underscore "_" character? In that case, the Rule would apply to your php page as well, and you might get a loop if any process makes another subrequest.

The solution (if this is the case) would be to add a condition to the rewrite:


RewriteCond %{REQUEST_URI} !^/script_page\.php$
RewriteRule ^([^_]+)_(.*)$ /script_page.php?m=$1\%20$2 [NE,L]

This stops the rule from being applied a second time if it has already been applied for the current HTTP request. Also note that I have added an 'L' flag to stop the rewriting engine after this rule has been applied.

Jim
<edit> Corrected missing space problem </edit>

[edited by: jdMorgan at 6:14 pm (utc) on Dec. 1, 2003]

8:23 am on Dec 1, 2003 (gmt 0)

Junior Member

10+ Year Member

joined:Nov 20, 2003
posts:144
votes: 0


yes, my page contains "_"

I have used your code but return Internal Server Error

But it's correct "REQUEST_URI"?

thks for your time :)

4:33 pm on Dec 1, 2003 (gmt 0)

Junior Member

10+ Year Member

joined:Mar 6, 2003
posts:109
votes: 0


I assume you are using Aston Martin to define m in page.php. If this is the case and you got the RewriteRule to work but need to convert the _ back to a %20 then you could do a str_replace at the top of page.php before your database query.

Something like this:

$newM = str_replace("_", " ", "$m");

Then call $newM in your database query, of course if you're not doing a query then this post is of no value :)

6:16 pm on Dec 1, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 31, 2002
posts:25430
votes: 0


webstyler,

See the correction to my post above - The missing space between "}" and "!" will cause a server error. Posting code on this forum removes a space preceding a "!", so you have to type two spaces. Try adding that space.

Jim

6:44 pm on Dec 1, 2003 (gmt 0)

Junior Member

10+ Year Member

joined:Nov 20, 2003
posts:144
votes: 0


:(

apache loop

7:50 pm on Dec 1, 2003 (gmt 0)

Junior Member

10+ Year Member

joined:Mar 6, 2003
posts:109
votes: 0


Try this and then use the str_replace I suggested in my last post

RewriteRule ^([[:alnum:].-]+)\.html$ page.php?m=$1