rewrite and url decoding - url

It seems a site is linking to mine in a bad way.
From google webmaster tools I see some 404 errors
domain.com/file.php?id=1 (404) Not found (Date)
This url works ok but because of browser page decoding, the real (404) url is
domain.com/file.php%3Fid%3D1 (this is what my browser displays in the url input when I click on google url)
My first try was
RewriteRule ^(.*)\%3F(.*)$ $1?$2 [R=301, L]
to change %3F to '?' but it does not work. It is confusing what is real and what is en/decoded.
Thanks

Hmm, try this:
RewriteRule ^(.*\%3F.*)\%26([^%]*)(\%3D)?(.*)$ $1&$2=$4 [N]
RewriteRule ^(.*)\%3F([^%]*)(\%3D)?(.*)$ $1?$2=$4 [R=301,L]
EDIT: I see you already tested for %3F... if that didn`t work, then something else might be up.

It is not an elegant solution, but after months, it is the only way I found.
Regular case
domain.tld/dir/file.php?id=UID&qty=NUM&... (from old site structure)
is rewritten as
domain.tld/newfile.php?id=UID&qty=NUM&... [R=301,L]
So, for requests like
domain.tld/dir/file.php%3Fid%3DUID%26qty=NUM%26... (from old site structure)
I perform
RewriteRule ^dir/file\.php(.*)$ script.php?qs=$1 [R,L]
This MUST be AFTER all directives that involves dir/file.php
Then I can handle the query string $_GET['qs'] in script.php
It may not apply to other situations that would enter into a loop or undesired results.

use [NE] flag in your RewriteRule

Related

how to create syntax for url rewriting

i have got this url like below :
http://www.mylink.com/property?id=34455
whats the rule to rewrite it to be in the following form without breaking
the css or anything. I see it done on multiple websites
http://www.mylink.com/property/3455
i already tried
RewriteRule ^property/?$ property.php [NC,L]
If the URL to be rewrited to http://www.mylink.com/property/3455 is http://www.mylink.com/property.php?id=34455 (notice the ".php" after property), the rule could be this one:
RewriteRule ^property/([0-9]+)$ property.php?id=$1
You can test your rules here http://martinmelin.se/rewrite-rule-tester/

Rewrite URL for 2 queries

I can't figure out how to have two queries in a URL rewrite.
I know that the following will allow me to have a single query.
RewriteRule ^movie/(.+)$ movie.php?url=$1
example.com/movie/superman
but how would i add another query in there?
example.com/cinema/vue-sheffield/movie/superman
everything i do seems not to work.
Thanks
If your cinema is always sheffield you can do this.
RewriteRule ^cinema/vue-sheffield/movie/([^/]+) index.php?movie=$1&cinema=sheffield&%{QUERY_STRING} [L]
if not then you can do this
RewriteRule ^cinema/([^/]+)/movie/([^/]+) index.php?cinema=$1&movie=$2&%{QUERY_STRING} [L]
Then you can have
example.com/cinema/whateverhere/movie/somemovie
That is not necessarily a query, that depends on the web service you're talking to – the stuff coming after the ? is the URL parameter list, and you can have multiple URL parameters in there separated by &. Usually, the order or parameters doesn't matter, and more often than not, you'll find them in the form ?foo=bar&etc=whatever.
I'm not sure though what your question on rewrite is.

.htaccess php progress

Thanks to the people that've helped me so far with this, I'm ready for the last step, I think.
I've got my URLs looking like this.
/brochure.php?cat_path=35&name=concrete-intermediate-posts
This is great and finally I just need to know how to turn that URL into this desired URL:
/brochure/35/concrete-intermediate-posts
Just like the Stack Overflow format.
Could anyone help me with the correct .htaccess rule?
Also, if I have other get variables in other sections, will this re-write harm them? (they use different variable names)
Thanks
With mod_rewrite you will rather do the opposite: rewrite a URL path like /brochure/35/concrete-intermediate-posts internally to /brochure.php?cat_path=35&name=concrete-intermediate-posts:
RewriteRule ^([^/]+)/(\d+)/([^/]+)$ $1.php?cat_path=$2&name=$3 [L,QSA]
The other side, using a URL path like /brochure/35/concrete-intermediate-posts instead of /brochure.php?cat_path=35&name=concrete-intermediate-posts in the HTML documents, would be done with PHP.
I Hope you mean something like this:
RewriteEngine On
RewriteBase /
RewriteRule ^brochure/([0-9]+)/([-a-zA-Z0-9]+)$ /brochure.php?cat_path=$1&name=$2 [L]

Htaccess RewriteRule trouble - How to redirect to a "re-written" url on the server?

I have the following rewrite rule which is working fine:
RewriteRule ^([a-zA-Z\-]+)[/]?$ apply/?survey_folder=$1
which turns this: address.com/folder1/
into this: address.com/apply/?survey_folder=folder1
The problem is that I can't figure out a way to use a redirect to the rewritten URL. Using cfml as an example, (cflocation url="http://www.address.com/folder1/") throws an error because, of course, the folder "folder1" doesn't actually exist on the server.
I can get the user redirected to the correct page by using /apply/?survey_folder=folder1, but that defeats the purpose of having the rewrite rule at all.
Is there any way to redirect to a URL rewritten by htaccess? I'm new at RewriteRule.
Add the [L] flag to the rule otherwise you're in danger of infinite loops and other problems.
The redirect you need is incredibly simple.
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /apply/(index\.php)?\?survey_folder=([\ ]+)\ HTTP/
RewriteRule ^apply/(index\.php)? http://www.example.com/%1 [R=301,L]
RewriteRule ^([A-Z-]+)$ /apply/?survey_folder=$1 [NC,L]
Do not let both URLs with trailing slash and URLs without trailing slash resolve to the same content. The URL for a page does not have a trailing slash. Add another preceding redirect to strip trailing slashes in that case.
Have you tried setting RewriteBase? Its purpose is to map the URL to the physical location on the server. I'm not completely sure this is the problem you are having, but it does sound like a possibility.
http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html#rewritebase

Mod_rewrite - how to 301 redirect an old URL to a new one

I need to grab some of my website's old URLs and do a 301 redirect to the new ones, since they are already indexed and we don't want to loose relevance after the change. The old URL is in fact very ugly and for some reason everything I try to do to rewrite it does not work. Here it is:
http://www.mywebsite.com/ExibeCurso.asp?Comando=TreinamentoGeral&codCurso=136&Titulo=Como%20Estruturar%20um%20Sistema%20Gerencial%20de%20Controles%20Organizacionais,13
Basically, I need to translate it into something like:
http://www.mywebsite.com/curso/136
From the old URL I need to check if the user typed "ExibeCurso.asp"; then I know I must send him here: /curso. I must also grab the integer that was in the querystring parameter "codCurso" (136). What is the regular expression I must use for this. I am using ISAPI_Rewrite 3, which basically implements htaccess on IIS, so there should be no difference in terms of syntax. Thanks.
Try this rule:
RewriteCond %{QUERY_STRING} ^([^&]*&)*codCurso=([0-9]+)(&.*)?$
RewriteRule ^/ExibeCurso\.asp$ /curso/%2? [L,R=301]
But I’m not sure whether ISAPI Rewrite requires the pattern to begin with a slash.
Off the top of my head, something like this should work:
RewriteRule ^ExibeCurso.asp(.*)$ http://www.mywebsite.com/curso/$1 [L,R=301]
That would at least send the traffic to /curso/ with all parameters attached. Maybe it's best to process it from there.

Resources