Question about a redirect - .htaccess

I just noticed that sometimes (even when given a wrong url) load perfectly fine. How do they accomplish this? What I mean is, suppose you click on a link that seems good like www.foo.com but it contains in the end a space character which would appear on the address bar as www.foo.com%20 some sites manage to redirect this to their correct url while others just break. How can this be achieved? I'm guessing it's something to do with the .htaccess but I have no idea what to do or where to do it.
The URL I'd like to redirect looks like this actually: http://foo.com/%C2%A0
I get the following error message:
The requested URL /%C2%A0 was not found on this server.
How can I make this redirection?
So far I came up with:
RewriteEngine on
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /[^%?\ ]*\%
RewriteCond %{REQUEST_URI} !^/
RewriteRule ^(.*)$ http://www.foo.com/ [R=301,L]
but it's not working at all

Related

htaccess is working but does not replace the url

I'm trying to modify the subdomain name in the URL to make it look nicer. My current URL look something like:
www.mystore.com/productInfo.php?cPath=11_11&productID=222
So, I want to make it nicer by Rewrite in .htaccess in main with this code:
RewriteEngine On
RewriteRule ^productInfo/([0-9_-]+)/([0-9_-]+) productInfo.php?cPath=$1&productID=$2 [NC,L]
When I run and test it on the URL by typing www.mystore.com/productInfo/11_11/222 in the URL it works well. However, when this page is redirected by a different page or is 'refreshed' with a self redirecting a href= link(in which the link is written in php by the previous programmer), the above old link is still visible in the URL instead of the new one.
I am still a beginner while I suspect that I might need to change something in the cPanel/Apache(I think) for this but currently, I am still do not have access to the cPanel control. Is there anything that I might have missed to write in the .htaccess file or I really do need the cPanel control or any other reasons?
Any help is appreciated. Sorry that I could not find similar questions on this.
You can use the following :
RewriteEngine On
RewriteBase /
#redirect /productInfo.php?cPath=foo&productID=bar to /productInfo/foo/bar
RewriteCond %{THE_REQUEST} /productInfo\.php\?cPath=([0-9_-]+)&productID=([0-9_-]) [NC]
RewriteRule ^ productInfo/%1/%2? [L,R=301]
#rewrite new URL to the old one
RewriteRule ^productInfo/([0-9_-]+)/([0-9_-]+) productInfo.php?cPath=$1&productID=$2 [NC,L]

Redirection with .htaccess, hiding the original URL

I have a domain, www.mydomain2.com, that I'd like being redirected to www.mydomain1.com/folder_site2/ but I'd like the URL that appears in the address bar being "www.mydomaine2.com" instead of the "www.mydomain1.com/folder_site2/"
After searches in the Apache documentation, I made my .htaccess looking like this:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain2\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/folder_site2
RewriteRule ^(.*)$ /folder_site2/$1 [L]
I first thought it was working fine, as, when I typed "mydomain2.com", I was correctly redirected on "www.mydomain1.com/folder_site2/" with "mydomain2.com" as visible URL.
But it seems it's impossible to reach a sub-folder. Any URL like "mydomain2.com/subfolder/text.html" would reach to a 404 (while it was possible to reach via "mydomain1.com/folder_site2/subfolder/text.html")
What have I done wrong? Thanks for your help!

Redirect all subdomains using .htaccess

What I want to achieve is to redirect any subdomain.mydomain.info to mydomain.info/subdomain using a 301 so that the visitor still sees subdomain.mydomain.info.
After some research I found that I had to set wildcard in my A-Record, did that. Than I went on to create a .htaccess. Below is my entire .htaccess.
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.domain\.info [NC]
RewriteCond %{HTTP_HOST} ([^.]+)\.domain\.info [NC]
RewriteRule ^(.*)$ /%1/$1 [L]
When I open subdomain.mydomain.info where I know that mydomain.info/subdomain is an existing folder I only get a message telling me that the domain "subdomain.mydomain.info" is unavailable.
My webspace is running a Confixx panel, just if that helps.
What could be going wrong here?
At this point I am guessing that some configuration outside the .htacces need to be made, but no idea what and where.
BIG EDIT:
Revisiting this. Turned out I had to talk to my provider to get some things set up correctly. Still trying to figure this our though.
Current situation: the .htaccess from above gives me a 500. Putting in an R, als was suggested in the comments, will redirect "sd.domain.info" to "domain.info/sd/sd/sd/sd" and result in an error by my browser. The browser says "There is redirect on this page" and give me the option to load it again. The version suggested by Al Kafri Firas also gives me a 500. When I remove the .htaccess any "subdomain.doamin.info" gets redirected to "domain.info" with the URL being changed in the head of my browser.
Still looking to get this working....
Revert all changes you made to your A-Record and use this rules
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.example\.info$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([a-z0-9-]+)\.example\.info$ [NC]
RewriteRule /%2%{REQUEST_URI} [PT,L]

.htaccess redirect to a single page everytime, but keep same url for application reasons

I looked through other stack overflow questions and I couldn't find one that was my exact case.
I have tried writing several different .htaccess rewrite rules but I can't seem to get it working.
I need to do the following:
Original URL: testexample.com/tool/1
Needs to redirect to: textexample.com/tool/display.php
But the URL in the browser needs to stay : testexample.com/tool/1
Can anyone point me in the right direction for rules for this rewrite?
You may try this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !display\.php/? [NC]
RewriteCond %{REQUEST_URI} ^/([^/]+)/?.*/? [NC]
RewriteRule .* %1/display.php [L]
Maps silently
http://testexample.com/anything/folder (Shown in the browser all the time)
To
http://textexample.com/anything/display.php

rewriting url to hide real page path

I recently with the help of SO solved my htaccess rewriterule issue:
Reuse subdomain in a rewrite rule
The result is that when somebody enters
whatever.example.com/anypage
in the adress bar, the htaccess automatically redirects to
whatever.example.com/somepath/whatever/anypage
What I wish to do is to find a way to just show whatever.example.com/anypage in the adress bar with the content of whatever.example.com/somepath/whatever/anypage displayed.
In the post I mentioned earlier, Jon Lin clearly mentions the following:
redirects always change what's in the browser's URL address bar
However I know some very frequent cases of url rewritting that would show in the adress bar let's say, for instance:
example.com/article-1-15
but actually showing the content of
example.com/somepath/somepage.php?article=1&otherparam=15
How could this apply to my case? I really wish to have a tiny url but it seems I missed something.
You may try something like this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/([^/]+)$
RewriteRule .* http://whatever.example.com/somepath/whatever/%1 [L]
It will map:
http://whatever.example.com/anypage
To a resource at:
http://whatever.example.com/somepath/whatever/anypage
showing always in the browser's address bar:
http://whatever.example.com/anypage
UPDATED
If whatever is dynamic and is the same in the substitution URI, here is another option:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ([^/]+)\.example\.com.*
RewriteCond %{REQUEST_URI} ^/([^/]+)$
RewriteRule .* http://%1.example.com/somepath/%1/%2 [L]
This will work as long as both "whatever" in the substitution path are the same. If they are not, the last "whatever" has to be hardcoded, like this:
RewriteRule .* http://%1.example.com/somepath/whatever/%2 [L]
There is no other way as the incoming URL doesn't have it.

Resources