Masked redirect to subfolder using .htaccess - .htaccess

I have a new version of my website in a subfolder. What I am trying to do is redirect all traffic from "mysite.com.au" to "mysite.com.au/new-wp".
While doing this I Would like the url in the address bar to keep showing "mysite.com.au" and not "mysite.com.au/new-wp".
I am also trying to make sure that all the pages under "mysite.com.au/new-wp" are served with the address bar showing "mysite.com.au/".
I have tried this:
RewriteEngine on
RewriteBase /
RewriteRule ^(?!new-wp/)(.*)$ /new-wp/$1
Now, I can see that when I type in "mysite.com.au" I get the page on "mysite.com.au/new-wp" and the url says "mysite.com.au". But for all other pages, I still see the "new-wp" in the url in the address bar.
I would really appreciate it someone could point me the right direction.
Best regards,
Sarah

As $1 stands for first matched group the right one would be the second i.e. $2:
RewriteRule ^(?!new-wp/)(.*)$ /new-wp/$2

Related

Redirect 301 via htaccess with wildcard

I hope I am asking this questions correctly, if I am not, please feel free to correct me.
I am trying to redirect 301 via htaccess file for all user profiles within the same site so for example. The first section shows the actual URLs I want to redirect from and to where, but this is only focused on 1 user account.
Redirect 301 /otsn/members/admin/my-orders/ /otsn/members/admin/shop/
I am thinking can I use the percentage symbol to make this redirect universal to all users, maybe I'm wrong, Can I do the following?
Redirect 301 /otsn/members/%/my-orders/ /otsn/members/%/shop/
I created a better user profile shop tab that has more capabilities, so I want to get rid of the old one and in case anyone tries to enter the old version of that profile page tab by entering the url manually, I want them sent to the new version for their profile shop page but I want this to happen with every user profile in the system.
Is this the correct method? or is there a better more efficient way of doing this?
Thank you
You may use this rule as topmost rule in your .htaccess or Apache config:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/([\w-]+)/members/([\w-]+)/my-orders/?$ [NC]
RewriteRule ^ /%1/members/%2/shop [L,R=301]
%1 is back-reference for the first value we are capturing in RewriteCond i.e. first pattern in (...) and same way %2 will represent second captured value in (...).

How to redirect a "bad" url that has a query string but does not start with a ? symbol?

There's a site that has had a bunch of bad links indexed and I've been asked to deal with it. There's one type of link that is giving me a headache:
http://www.example.com/category-display.html&Category_Code=some_cat_code
I tried redirecting to the home page:
Redirect 301 /category-display.html& /
That doesn't work because it adds everything past the & to the url.
In the best of worlds, I'd like to redirect to:
/app/mm.mvc?Category_Code=some_cate_code
So I tried using querystring and RewriteRule/RewriteCond but there's no query string without the ? that I can figure out, so I'm kind of stuck here.
Any ideas?
You can use this rule as your top rule in site root .htaccess:
RewriteEngine On
RewriteRule ^category-display\.html&(.*)$ /app/mm.mvc?$1 [L,NC,NE,R=301]

.htaccess rewrite url that has already been changed

I am upgrading my site which involves new scripts and a different URL
structure. There are currently a few thousand pages so I want to
basically move them in to a subdirectory so that they are not lost.
I am not very confident with htaccess so can someone please confirm that
the first part I have done is correct:
Old URL: http://www.example.com/article/another-dir/page-group/whatever.html
RewriteRule ^article/?$ http://www.example.com/archive/ [R=301,NC,L]
To achieve this: http://www.example.com/archive/another-dir/page-group/whatever.html
Search engines will see the above as a permanent move and in the address bar
it will show the new url. Is this right?
The second part is on the new script - the url's could be improved but I am
unable to change all the script so thought about using htaccess again but am
not sure if it can be achieved like this.
At the moment the url looks like this:
url: http://www.example.com/category/4/categoryname
In the htaccess the current rewrite rule for this type of url looks like this:
RewriteRule ^category/(.*)/(.*)$ category.php?id=$1&slug=$2
Is it possible to change this so that in the url address bar I end up
with this:
http://www.example.com/categoryname
Basically I don't want to see either the number or category/ in the resulting
url as it will be easier for visitors to use - is that possible??
Thanks in advance for any help.
The second question related to passing in URI components for querystring redirect and then hiding those components in the URL I don't think would be easy, if even possible, using RewriteRules.
For the first question though. Given the sample URLs you mentioned, the RewriteRule would need to include capture and backreference if you want to preserve the full URL in the redirection. For example:
RewriteRule ^article/?(.*)$ http://www.example.com/archive/$1 [R=301,NC,L]

htaccess - Is this Correct?

Have looked through various articles both here and elsewhere but
could do with confirmation regards the way I have set up htaccess.
RewriteEngine On
RewriteCond %{HTTP_REFERER} orange [NC,OR]
RewriteRule .* blocked.php [L]
Can someone please confirm that given the code above that it will
block any domain, subdomain or page with the word orange in it?
It looks to be working but I need to ensure that I have covered all
bases and that I am blocking any link from or any image hotlinked from
my site where there is orange anywhere in the domain.
BTW I thought that if I have only one condition that I can leave out
the ,OR but when I removed it then it did not work in the test I was
doing??? Anyone know why this would not work without the ,OR ??
Many thanks in advance!
Can someone please confirm that given the code above that it will block any domain, subdomain or page with the word orange in it?
Yes, it will rewrite all requests to the /blocked.php script as long as "orange" is in the referer. The "Referer" header is what browsers typically include in a request letting the webserver know what page/site they were just at that linked to the resource that they're requesting. That means if there's a site called "orange.com" that has a page that links to one of your pages, and someone clicks on it, the referer will contain that orange.com page and the rules you have will block them.
You don't need the OR flag. Leaving it out works for me when the referer contains the word "orange". Just keep in mind that referers can be spoofed and it isn't a guarantee.

How to redirect an erroneous URL

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
URL Rewrite would be the IIS version that may exist in other forms if you want to look at re-writing the URL assuming you mean this kind of case.
Don't forget that browsers may make certain guesses about what someone enters so that if someone types in "foo.com " that the browser may trim white space by default rather than URL encode the text. If "http://foo.com" fails then it may try "http://www.foo.com" for another idea as these could be seen as simple interpretations to take on what someone types in. If both fail then it may just Google the text believing that the address bar should be treated like a search box.

Resources