Forum Moderators: phranque

Message Too Old, No Replies

How To Remove Tilde from urls

How To Remove Tilde from urls by htaccess

         

studee

9:35 pm on Sep 13, 2011 (gmt 0)

10+ Year Member



hi,

i am facing a problem with one of my website. somehow the urls which google bot is indexing contains my hosting account username/directory name.

let me explain my site mysite.com have directory name "/home/mysite/"

google is indexing the urls as,


http://mysite.com/~mysite/index.php?var1=var


the pages are loading fine

i could not find why and how google is indexing this,

however i want to redirect urls like that


from http://mysite.com/~mysite/index.php?var1=var
to http://mysite.com/index.php?var1=var


can anyone help me with this ?

my current .htaccess have these values



Options +FollowSymLinks

RewriteEngine On

RewriteCond %{HTTP_HOST} !^mysite.com$ [NC]
RewriteRule ^(.*)$ http://mysite.com/$1 [L,R=301]



regards
Dee

g1smd

10:09 pm on Sep 13, 2011 (gmt 0)

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



Something like this?

RewriteRule ^~mysite/(.*) http://example.com/$1 [R=301,L]


RewriteCond %{HTTP_HOST} !^(example\.com)?$
RewriteRule (.*) http://example.com/$1 [R=301,L]


That's two rules, one which fixes only ~mysite requests, followed by one which fixes all other non-canonical requests.

Every code change from your example is important.

studee

10:24 pm on Sep 13, 2011 (gmt 0)

10+ Year Member



hi g1smd,

Thanks for reply ,

your solution is not working :( for me still landing on same page with /~mysite/ in url

Dee

g1smd

10:43 pm on Sep 13, 2011 (gmt 0)

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



The first rule will need to go in a higher level folder, above the /~mysite/ folder.

lucy24

11:01 pm on Sep 13, 2011 (gmt 0)

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



Does it make any difference if you say (~|%7E) instead? That's the percent-escaped version of the tilde character. Either put in the no-capture tag (?:~|%7E) or change the $1 to $2.

tangor

2:37 am on Sep 14, 2011 (gmt 0)

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



You'll have to have a higher folder access for the htaccess to defeat the shared hosting... or move to a different host.

lucy24

3:35 am on Sep 14, 2011 (gmt 0)

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



I don't think you need to go upstairs. The original problem was how to replace anything in the form

http://example.com/~mysite/index.php?var1=var

with

http://example.com/index.php?var1=var

in other words, get rid of the first "directory". Doesn't matter that there's no such directory, since as somebody somewhere once mentioned, urls and paths are different critters.

What happens if you try simply

RewriteRule (~|%7E)mysite(.*) http://example.com$2 [R=301,L]

?

studee

5:54 am on Sep 14, 2011 (gmt 0)

10+ Year Member



Hi ,

The first rule will need to go in a higher level folder, above the /~mysite/ folder.


mysite is the folder which is the main folder for my hosting account, to add further i am on a VPS the site is located on

"/home/mysite/public_html"

What happens if you try simply

RewriteRule (~|%7E)mysite(.*) http://example.com$2 [R=301,L]



This is also not working .


Dee

g1smd

6:33 am on Sep 14, 2011 (gmt 0)

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



How about this?

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /~mysite
RewriteRule (.*) http://example.com/$1 [R=301,L]

RewriteCond %{HTTP_HOST} !^(example\.com)?$
RewriteRule (.*) http://example.com/$1 [R=301,L]

studee

7:01 am on Sep 14, 2011 (gmt 0)

10+ Year Member



hi g1smd,


How about this?

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /~mysite
RewriteRule (.*) http://example.com/$1 [R=301,L]

RewriteCond %{HTTP_HOST} !^(example\.com)?$
RewriteRule (.*) http://example.com/$1 [R=301,L]



Finally this worked ;)

thanks everyone for all help

Dee

lucy24

8:42 am on Sep 14, 2011 (gmt 0)

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



Whew :)

g1, before you go away, can you explain why

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /~mysite
RewriteRule (.*) http://example.com/$1 [R=301,L]

doesn't just throw back the same /~mysite address you started with?

More trivially: Since the two rules are identical, would it still work if you collapsed it to

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /~mysite [OR]
RewriteCond %{HTTP_HOST} !^(example\.com)?$
RewriteRule (.*) http://example.com/$1 [R=301,L]

? Or does the /~mysite option have to be attacked separately before you can do anything else?

g1smd

5:25 pm on Sep 14, 2011 (gmt 0)

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



For readability, two separate rules are probably better.

Rule 1 is a kludge. The rule is located in ~mysite/.htaccess. Leading folders are stripped as the path presented to mod_rewrite is localised per-directory. So the Rule cannot see the "~mysite" part. You want the rule to run when "~mysite" appears in the request and that is exactly what the RewriteCond does.

Make sure that the rule continues to work as expected. It is possible that the host will alter the server configuration at some time in the future and the requests will no longer resolve.