I need to redirect multiple URLs with% character, through htaccess.
Currently the lines are:
RewriteEngine On
RewriteCond% {REQUEST_FILENAME}!-F
RewriteCond% {REQUEST_FILENAME}!-D
RewriteRule ^-.*$ url [R=301,L]
RewriteRule ^%20.*$ url redirection [R=301,L]
RewriteRule ^%22.*$ url redirection [R=301,L]
RewriteRule ^images/files/myname%is-fyle.pdf.*$ url redirection [R=301,L]
From these lines, the redirects do not work are:
RewriteRule ^%20.*$ url redirection [R=301,L]
RewriteRule ^%22.*$ url redirection [R=301,L]
RewriteRule ^images/files/myname%is-fyle.pdf.*$ url redirection [R=301,L]
Any ideas on what I'm doing wrong? My knowledge is basic almost, not quite understand why the redirects with % in the URL do not work.
I researched on the use of flags as noescape [NE], but I have not managed to get the redirects from the URLs with the character %.
("url redirection" in te lines of htaccess is the URL that being new to the forum I can not post).
Thank you.
Sep 23: I thank those who have helped me with this problem. However, the URLs with special characters (% and ?) and capital letters do not forward.
I copy and paste the complete code for my htaccess file:
Here: http://pastebin.com/yXGQiQzd
I saw in this post How to Redirect to a url containing an anchor (#)? that apparently the answer is posted. However, the matter is in the use of ([]) for the rule. The example is: ([a-zA-Z0-9-_]+)
In my case, for example, these URLs:
images/SchoolContry/wef/pdf/SomethingOfFiles/the reasons behind?s of the ca?da.pdf
images/stories/Money/memo Matric?cula 2010.doc
Any idea why URLs with special characters (% and ?), space and capital letters redirect not?
Thanks.
URL encoded characters get decoded before they're sent through the rewrite engine. That means you need to use the decoded characters to match against and not the encoded ones. So for:
RewriteRule ^%20.*$ url redirection [R=301,L]
you want:
RewriteRule ^\ .*$ url redirection [R=301,L]
and for:
RewriteRule ^%22.*$ url redirection [R=301,L]
you want:
RewriteRule ^\".*$ url redirection [R=301,L]
Related
I have a problem with 301 redirect in .htaccess file.
Yes it should be done in htaccess.
I have friendly link with non friendly tail with utf-8 characters:
https://example.com/plotno-16x24?tag=Fotobraz na płótnie
Im tryin to redirect it to
https://example.com/404 with code:
RewriteRule ^plotno-16-24?tag=([^?]*)$ /404 [R=301,NE,NC,L]
But it works only before question mark, after this sign "?" it always fail.
Any ideas ?
? or part of QUERY_STRING is not matched in RewriteRule. You may use this rule:
RewriteEngine On
RewriteCond %{THE_REQUEST} /plotno-16-24\?tag=. [NC]
RewriteRule ^ /404? [R=30,L,NE]
Also note use of ? after /404 that strips off any pre-existing query string.
I have a htaccess file which is used for earning money by short links, as the following code:
RewriteEngine On
RewriteRule (.*) http://shortlink.com/s/N9IpzM7J?s=$1 [R=301]
But with above code, it'll redirect example.com/https://google.com to http://shortlink.com/s/N9IpzM7J?s=https:/google.com notice it that it lost a slash at https://, in my thought, it might be a special character in htaccess but I don't know how to escape it.
So I want to ask how to let the above code works which will redirect example.com/https://google.com to http://shortlink.com/s/N9IpzM7J?s=https://google.com?
Because of this post I replace by link shortening service's url to shortlink.com!
If you want to capture full URI, you should use RewriteCond directive.
Apache automatically strips off multiples slashes into a single slash in RewriteRule directive.
RewriteCond %{REQUEST_URI} ^/(.*)$
RewriteRule ^ http://shortlink.com/s/N9IpzM7J?s=%1 [R=301,L]
Looking to rewrite a part of a URL and stuck with dealing with special characters.
original
http://www.testwebsite.com/Products/Apple/*/!Accessories
desired result
http://www.testwebsite.com/Products/Apple-Accessories
I would also like to redirect (can a word be removed?) testwebsite.com/Products/Dell-Laptop/*/!Accessories
to testwebsite.com/Products/Dell-Accessories
You can use this rule in your root .htaccess:
RewriteEngine On
RewriteRule ^(Products/[^/]+)/\*/!(.+)$ /$1-$2 [R=301,L,NE]
At the moment we have one page which shows a list of links
each link has got its own ID number
each link gets opened with the file info.php?ID=X
for example:
www.mysite.com/info.php?ID=1 shows the link "weather italy"
www.mysite.com/info.php?ID=2 shows the link "weather france"
Since we have several links for "weather italy" and "weather france" we would like to rewrite new urls (weather-italy and weather-france) in .htacces
Whith the new urls we would have the folowing structure:
www.mysite.com/weather-italy/info.php?ID=1
www.mysite.com/weather-france/info.php?ID=2
With the following code we tell the server to rewrite the urls and call the original file:
RewriteRule ^weather-italy/info.php?$ info.php [NC,L]
RewriteRule ^weather-france/info.php?$ info.php [NC,L]
This works fine.
To avoid double indexing we want to redirect 301 the old link to the new link.
We have achieved that with the following code:
RewriteCond %{THE_REQUEST} \?ID=1
RewriteRule ^info\.php$ http://www.touristinfo.fr/weather-italy/info\.php [L,R=301]
RewriteCond %{THE_REQUEST} \?ID=2
RewriteRule ^info\.php$ http://www.touristinfo.fr/weather-france/info\.php [L,R=301]
This also does the job but combined with the first part of the script is produces a never ending loop.
What is wrong with our code?
Thanks a lot for your help :)
%{THE_REQUEST} is supposed to only match if the url you want to match is an external request. Your problem is that the regex you made is not specific enough.
Let's examine what happens. You go to example.com/info.php?ID=2. The first two rules don't match, but the 4th one does. You end up with a redirect to example.com/weather-france/info.php?ID=2.
This goes through your .htaccess again. The second rule matches, and internally rewrites it to info.php?ID=2. The [L] flag doesn't make a difference here, because the url will be pulled through .htaccess until it stops changing. On the second cycle through .htaccess, the url will now match the 4th rule, even though the external request contained /weather-france/info.php?ID=2. ID=2 is in the external request too, and the internal rewrite is now info.php again.
The fix is to make %{THE_REQUEST} match enough so that the rewritten url doesn't match it anymore.
On a further note: Familiar yourself with the difference between regex and strings in RewriteRules and RewriteConds. You escaped a dot in a string, while leaving a dot in a regex unescaped. The ? is "match the previous character 0 or 1 times", not a question mark literal. The query string cannot be matched in the first argument of RewriteRule.
You'll end up with:
RewriteRule ^weather-italy/info\.php$ info.php [NC,L]
RewriteRule ^weather-france/info\.php$ info.php [NC,L]
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /info\.php\?ID=1
RewriteRule ^info\.php$ http://example.com/weather-italy/info.php [L,R]
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /info\.php\?ID=2
RewriteRule ^info\.php$ http://example.com/weather-france/info.php [L,R]
I have an htaccess file with several redirects, Now I want to create a pretty url for some link. I tried the following sentence and it does nothing:
RewriteEngine On
RewriteRule ^/example1.html /?page_id=100 [NC]
When I type www.MyDomain.com/?page_id=100 in my browser, the site shows, but the url still looks the same as I typed it. How would I change my sentence to show example.html in the url-bar instead of ?page_id=100 ?
Thanks in advance
You have a rule that says:
When the browser requests: /example1.html
Then show them: /?page_id=100
It goes one way, it doesn't do anything if the browser requests /?page_id=100. If you want to do something about a browser requesting the query string URL, *you need a rule to tell mod_rewrite to do it*:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(index\.php)?\?page_id=100($|\ |&)&?([^\ ]*)
RewriteRule ^ /example1.html?%3 [L,R=301]
Also, you may want to get rid of the / in the pattern of your rule:
RewriteRule ^example1.html /?page_id=100 [NC]
Leading slashes are stripped off when matching against rules in an htaccess file.