Forum Moderators: phranque

Message Too Old, No Replies

replacing spaces and quotes

how to do so in .htaccess

         

BlueBlizz

2:58 pm on Jul 23, 2009 (gmt 0)

10+ Year Member



Hello, I'm a newby on htaccess.

I want the following url to be rewriten:
[111.222.333.444...]

to
[111.222.333.444...]

or more generaly:
How can I replace ALL given character with something else in the complete URL even if there are /../../ before the part it concerns?

My .htacces :

# REMOVING QUERY STRING
RewriteCond %{QUERY_STRING} .
RewriteCond %{REQUEST_URI} .
RewriteRule (.*) c?%1

# TO LOWERCASE
RewriteCond %{REQUEST_URI} [A-Z]
RewriteCond %{REQUEST_URI} !\.(gif¦jpg¦jpeg¦png¦css¦js¦ico¦swf¦mov¦qt¦mp3¦rm¦mpg¦mpeg¦avi¦pdf¦doc)$
RewriteRule (.*) ${lc:$1} [R=301,L]

#SPACES AND QUOTES
RewriteRule ^(.*)\ ([^\ ].*)$ $1-$2 [E=rspace:yes,N]
RewriteRule ^(.*)\"([^\ ].*)$ $1$2 [E=rspace:yes,N]

# TRAILING SLASH
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ [111.222.333.444...] [L,R=301]

a url WITHOUT /c/ like
[111.222.333.444...] and gentleman"
returns:
[111.222.333.444...]

but WITH a trailing / it refuses to work:
[111.222.333.444...] and gentleman"/
returns:
[111.222.333.444...]

even
[111.222.333.444...] and gentleman"
does not rewrite my url

Any ideas?

jdMorgan

4:18 pm on Jul 23, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Aside from some problems in the code above, some of which appears to have been copied and pasted without research, there is a known bug with Apache when it internally rewrites a URL multiple times. This may be the cause of your problem.

I posted a case-conversion routine as part of a much larger piece of code [webmasterworld.com] in our forum library that may be of some help to you. But be advised that using mod_rewrite to do case-conversion and character substitution is *highly* inefficient. If you have other options like using a RewriteMap (as you did above with "${lc:$1}") or doing the character-substitution and redirects in a script, I highly-recommend those alternatives. Do be sure that you've got the "lc" lowercasing map defined, as it is not necessarily going to be available on any shared server.

Jim

BlueBlizz

7:13 am on Jul 24, 2009 (gmt 0)

10+ Year Member



just a simple rewrite?

[111.222.333.444...]
to
[111.222.333.444...]

but how to do so in .htaccess apache(2.0)?

jdMorgan

1:24 pm on Jul 24, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Why would you want to rewrite like that? It is entirely backwards.

Normally, you want the server to internally rewrite the browser's incoming HTTP request for the new URL-path /c/Biking/bike-tools to the server filepath /c/Biking/%22bike%20-%20tools%22
And then, if there are old links out on the web to the dynamic URL-path, you want the server to externally redirect incoming requests for the old URL-path /c/Biking/%22bike%20-%20tools%22 to the new URL http://example.com/c/Biking/%22bike%20-%20tools%22

Before doing either of these, the links on all of your pages must be changed to http://example.com/c/Biking/bike-tools. You must edit the HTML or modify the script that produces your HTML pages -- mod_rewrite cannot change your HTML.

Jim

BlueBlizz

2:08 pm on Jul 24, 2009 (gmt 0)

10+ Year Member



I want to delete the spaces and quotes.
We have changed our pagename from "bike - tools" into
bike-tools.

We want the current google links (and the corresponding page rate) to be redirected to the new page.
So all urls google knows (like "bike - tools") have to be directed to "bike-tools" our new page.
This is only a temporary issue. Google reads the new page via a robot spider and the current stored in it's ranking list. Both have to be pointed to the same page. Otherwise 2 url are going to the same page and reduces the raking

That's why the spaces and quotes have to be deleted.

jdMorgan

2:28 pm on Jul 24, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Then that is a redirect not a rewrite. Big difference.

Take a look at the code I cited above. You can modify it to remove quotes and spaces.

Jim

BlueBlizz

8:32 am on Aug 19, 2009 (gmt 0)

10+ Year Member



I've put next code into the httpd.conf (htaccess is not possible)
just a small change of you code to change W into w

RewriteCond %{ENV:myURI} ^([^\"]*)(.*)$
RewriteRule . - [E=myURI:%1%2]
RewriteCond %{ENV:myURI} ^([^\ ]*)(.*)$
RewriteRule . - [E=myURI:%1-%2]
RewriteCond %{ENV:myURI} [" ]
RewriteRule . - [N]

NO RESULTS !

a hardcoded rewriterule:

RewriteRule [domain.com...] jacks" [domain.com...] [R=301,L]
doesnot work either. No url change :-(

g1smd

6:54 pm on Aug 19, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You're just guessing at code here. That can never work.

The pattern should not contain http or the domain name.

RewriteRule cannot 'see' query strings. You need a RewriteCond for that.

Another major problem is that for most of your post you were asking for a 'rewrite' and at the last minute you have started to show code for a 'redirect'.

You need a redirect for any requests asking for the old URL so that the user is redirected to the new URL.

This other thread talks about both redirects and rewrites: [webmasterworld.com...]

jdMorgan

4:04 am on Aug 20, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



An example of this would be:

RewriteCond %{QUERY_STRING} ^ObjectPath=/Shops/domain/Categories/Outdoor/outdoorkleding/outdoorkleding/"outdoor\%20jacks"$
RewriteRule ^nl_NL/$ http://example.com/c/outdoor/outdoorkleding/outdoorkleding/outdoor-jacks? [R=301,L]

Jim

BlueBlizz

8:47 am on Aug 20, 2009 (gmt 0)

10+ Year Member



I've got 2 different kind of url's:
A:
[domain.com...]
and
B:
[domain.com...] kids"
(A= without and B= with spaces and quotes)

I'm using Apache 2.0 on Linux RedHat 4

A gives me no problems to redirect to
[domain.com...]

B has to have as result:
[domain.com...]
But refuses

Either I do a redirect of the hardcoded url inside httpd.conf OR I have to have a piece of code to change in the QUERY_STING the spaces into dashes and delete the quotes

I prefer the second on, but I cannot find a solution.
So I also tried the hardcodes way.

already tried without positive result:

1. RewriteCond %{QUERY_STRING}^ObjectPath=/Shops/domain/Categories/Outdoor/"chairs\%20kids"$
RewriteRule ^nl_NL/$ http://example.com/c/outdoor/chairs-kids? [R=301,L]
=== it still shows the old url in address-bar

2. RewriteCond %{QUERY_STRING} ^ObjectPath=/Shops/domain/Categories/Outdoor/%22chairs%20kids%22"$
RewriteRule ^nl_NL/$ http://example.com/c/outdoor/chairs-kids? [R=301,L]
=== it still shows the old url in address-bar

3. RewriteCond %{QUERY_STRING} ^ObjectPath=/Shops/domain/Categories/Outdoor/"chairs kids"$
RewriteRule ^nl_NL/$ http://example.com/c/outdoor/chairs-kids? [R=301,L]
=== page error or all url (also otherones)

I'm running Apache 2.0 on Linux RedHat 4

jdMorgan

7:37 pm on Aug 20, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Do you have any working RewriteRules at all on this server?

Have you enabled the Options that mod_rewrite requires (either "Options +FollowSymLinks" or "Options +SymLinksIfOwnerMatch" as documented) and included the "RewriteEngine on" directive in your .htaccess file?

You might want to examine the actual HTTP requests by using the "Live HTTP Headers" add-on for Firefox. This might help to determine which characters are being encoded and which are not, and to determine whether any characters are getting double-encoded. For example: " --> %22 --> %2522

Jim

BlueBlizz

2:21 pm on Aug 21, 2009 (gmt 0)

10+ Year Member



this one works:

RewriteCond %{QUERY_STRING} ^ObjectPath=/Shops/domain/Categories/Outdoor/%22chairs\%20kids%22$
RewriteRule ^(.*)$ [domain.com...] [R=301,L]

jdMorgan

5:42 pm on Aug 21, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



OK, so when the URL is in "SEO-friendly" form, the spaces and quotes are escaped. So you might try something like this, even though --as I stated above-- it is quite inefficient due to the fact that mod_rewrite processing must be re-started for each occurrence of a quote or a space in the requested URL-path:

# If character replacement not already in progress
RewriteCond %{ENV:Replace} !=Yes
# and if the client-requested URL-path is not in the relevant
# format or if it contains no escaped quotes or spaces
RewriteCond %{THE_REQUEST} !^[A-Z]+\ /[a-z]/[^/]+/[^%]*\%(25)*2[02][^\ ]*\ HTTP/
# then skip the next five rules
RewriteRule ^ - [S=5]
#
# Else if character replacement not already in progress and the client-requested URL does
# contain escaped quote or space characters, copy the client-requested URL-path to a user-
# defined variable so that we can work on it without triggering Apache's known multiple-
# consecutive-rewrite error, and set the "character-replacement-in-progress" flag
RewriteCond %{ENV:Replace} !=Yes
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([a-z]/[^/]+/[^%]*\%(25)*2[02][^\ ]*)\ HTTP/
RewriteRule ^ - [ENV=myURL:%1,E=Replace:Yes]
#
# Remove one escaped quote (if any) from the working copy of the client-requested URL-path
RewriteCond %{ENV:myURL} ^([a-z]/[^/]+/[^%]*)\%(25)*22([^\ ]*)\ HTTP/
RewriteRule ^ - [E=MyURL:%1%3]
#
# Replace one escaped space (if any) with a hyphen in the working copy of the client-requested URL-path
RewriteCond %{ENV:myURL} ^([a-z]/[^/]+/[^%]*)\%(25)*20([^\ ]*)\ HTTP/
RewriteRule ^ - [E=MyURL:%1-%3]
#
# (Note that either one or both of the two preceding rules will always be invoked if this section is executed)
#
# If the working copy of the client-requested URL-path still contains additional escaped
# quote or space characters, then restart mod_rewrite rule processing from the beginning
RewriteCond %{ENV:myURL} ^%{ENV:myURL} ^[a-z]/[^/]+/[^%]*\%(25)*2[02][^\ ]*\ HTTP/
RewruteRule ^ - [N]
#
# Else redirect the client to the cleaned-up URL
RewriteRule ^[a-z]/[^/]+/ http://www.example.com/%{ENV:myURL} [R=301,L]

Note that in order to prevent processing the RewriteRules and RewriteConds unnecessarily, I have made the code as selective as possible. It will only be run if the requested URL-path is in the form "/<lowercase-letter>/<any-string-up-to-the-first-slash>/<any-string-with-escaped-characters>"

You should place this code as close to the top of your existing rewriterules and .htaccess code as possible, consistent with the recommended practice of placing all external redirect rules first, in order from most-specific to least-specific, followed by all internal rewrites, again in order from most-specific to least-specific.

I can't guarantee that this is exactly what you need or even that it will work. I just typed it here, and though I eyeballed it several times for correctness, it's not tested. So no warranty... :)

At the very least, it should provide you with a fairly good start.

Jim

BlueBlizz

1:06 pm on Sep 1, 2009 (gmt 0)

10+ Year Member



even a simple redirect does not work when the condition deals with a query_string.

RewriteCond %{QUERY_STRING} ObjectPath=/ERIC$
RewriteRule (.*) [google.nl?...] [R=301,L]

dealing with request_uri works fine:
RewriteCond %{REQUEST_URI} /ERIC
RewriteRule (.*) [google.nl?...] [R=301,L]

BlueBlizz

1:15 pm on Sep 4, 2009 (gmt 0)

10+ Year Member



Thanks for your examples. They are a great help to me.

Although it works fine in our test system.
In the Live system, the flag [N] does not work. It gives a httpd syntax error.

Are the some special things I have to set?

jdMorgan

1:26 pm on Sep 4, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you are placing this code into httpd.conf outside of any <Directory> continaer, then it may have to be modified. In addition, if you have access to httpd.conf there is a better option available: Use a RewriteMap to call a small PERL script to 'clean up' the requested URL+query string instead of using a recursive (so slow) RewriteRule.

Jim

BlueBlizz

1:39 pm on Sep 4, 2009 (gmt 0)

10+ Year Member



This is part of the httpd.conf file.
it 's place between
<IfModule rewrite_module>
...
RewriteRule ^ - [next]
...
</IfModule>

removing the <ifmodule> tag does not help.

besides the test config is almost identical t the live config

BlueBlizz

1:48 pm on Sep 4, 2009 (gmt 0)

10+ Year Member



Rewrite the whole stuf in perl is not an option to me.
It has to be active on sunday and this rule is the only obstruction.

jdMorgan

2:19 pm on Sep 4, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> In the Live system, the flag [N] does not work. It gives a httpd syntax error.

What does the relevant entry in your server error log file say?

Jim