How to keep website available to some users oonly using htaccess - .htaccess

my website is under construction and i would like to show the website to my friends ,And deny to all users. i used particular ips in htaccess files,but ip is not static . Is there any way to keep mac address ? or any way

You can't match against a MAC Address using directives in an htaccess file. You could either
Put a password on your actual content and everyone without the password gets forwarded to an "Under Construction" page (possibly using ErrorDocument 403 /under_construction.html`)
Look for a special query string that you can give out to only your friends:
RewriteEngine On
RewriteCond %{QUERY_STRING} !special_var=my_buddies
RewriteRule ^ /under_construction.html [R=301,L]
So here, any url without the special_var=my_buddies query string ( http://hostname.com/some/content.php?special_var=my_buddies ) gets redirected to the under construction page
You can do something similar by setting a cookie. Create a small landing page called something like /my_buddies.php that simply does a set_cookie() call to create a my_buddies cookie (doesn't have to be php, and the cookie can be called anything). Then do something similar as the query string:
RewriteEngine On
RewriteCond %{HTTP_COOKIE} !my_buddies
RewriteRule ^ /under_construction.html [R=301,L]
Essentially the same thing. If there's no my_buddies in the cookie string, redirect to the under construction page.
There's probably other ways to get do something like this, but can't think of anything off the top of my head.

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 (...).

htaccess redirect if the url does not contain a specific character

I'm moving the site to a subdomain and need certain tag strings to go to the subdomain and some to remain on the main site. Problem is both have a similar tag system.
I need this type of request
https://www.site.co.uk/tags/example-tag
to go here:
https://sub.site.co.uk/tags/example-tag
but this type of request
https://www.site.co.uk/tags/view?tags=14-some-varriable
to remain unchanged and parsed to content without redirecting.
What would be the most recommended and best solution?
I have written some code to work around other redirects but this one is causing me a headache.
Cheers
For your this mentioned example, below should work.
RewriteEngine On
RewriteCond %{REQUEST_URI} ^(.+)/(.+)
RewriteRule ^ https://sub.site.co.uk/%1/%2 [R]

how to redirect URL containing query string and random numbers in .htaccess

When customers cancel a transaction on my site, they get redirected to the WooCommerce cart page with a query string containing randomly generated numbers at the end.
Example
https://www.example.com/cart/?woo-paypal-cancel=true&token=EC-5474842406066680S
(I need this redirect due to a plugin conflict between WP Rocket cache with CDN activated and WooCommerce. Long story.)
I'm wondering what exactly I would put in my .htaccess file to get it to redirect to
https://www.example.com/cart/
I've tried a number of variations I found on multiple pages here on Stackpath, but it wasn't redirecting. Obviously I'm missing something so I'm turning to the gurus.
Would be very grateful for your help.
To redirect /cart/?woo-paypal-cancel=true&token=<anything> to /cart/ you can try something like the following near the top of your .htaccess file (using mod_rewrite):
RewriteEngine On
RewriteCond %{QUERY_STRING} ^woo-paypal-cancel=true&token=
RewriteRule ^cart/$ /cart/? [R,L]
The ? on the end of the RewriteRule substitution strips the query string from the request.
This is a temporary (302) redirect.

URL Rewriting based on form input

I'm creating a frontpage for my website with a single form and input text, Google-style. It's working fine, however, I want to generate a pretty URL based on the input. Let's say, my input is called "id", and using the GET method of form, and the action defined to "/go/", on submission, the URL will be:
site.com/go/?id=whateverIType
and I want to change it to
site.com/go/whateverIType
I was thinking on Mod Rewrite, but if the user put something in the URL, like:
site.com/go/?dontwant=this&id=whateverIType&somemore=trash
I want to ignore the other variables but "id", and rewrite the rule.
What's the better way of get this done? Thanks in advance!
PS: I'm using CodeIgniter, maybe there's something I can use for it as well. I already have a controller for "go".
I'm not familiar with CodeIgniter, but you can try the following RewriteRule
RewriteEngine on
RewriteCond %{REQUEST_URI} ^\/go\/
RewriteCond %{QUERY_STRING} id=([^&]*)
RewriteRule (.*) /go/%1? [L,R]
The %1 references the regex group from the previous RewriteCond, and the trailing ? will strip the querystring from the redirected URL.
Hope this helps.
Mod_rewrite supports conditions and rules with RegEx, so you could have a rule that matched the ?id=XXXX, that would extract it from the URL (keeping the other parameters), and rewrote the URL accordingly.
However... I don't think you want to do this, because if you rewrite the URL to be /go/Some+Search+Query, you won't be able to pick it up with say, PHP, without parsing the URL out manually.
It's really tough to have custom, SEO-friendly URLs with user input, but it is technically possible. You're better off leaving in the ?id=XXX part, and instead, using mod_rewrite in the opposite approach... take all URLs that match the pattern /go/My+Search+Terms and translate that back into something like ?id=My+Search+Terms, that way you'll be able to easily parse out the value using the URL's GET parameters. This isn't an uncommon practice - Google actually still uses URL parameters for user input (example URL: http://www.google.com/search?q=test).
Just keep in mind that mod_rewrite rewrites the URL before anything else (even PHP), so anything you do to the URL you need to handle. Think of mod_rewrite as a regular expression-based, global "Find and Replace" for URLs, every time a page is called on the server. For example, if you remove the query string, you need to make sure your website/application/whatever accounts for that.
In application/config/routes.php
$route['go/(:any)'] = "go/index/$1";
Where go is your controller and index is the index action.
http://codeigniter.com/user_guide/general/routing.html
You can use something like this in your .htaccess if you aren't already:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|css|js|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]

How can I append HTTP_REFERER to query string using htaccess?

In my .htaccess file I have a set of rules as follows:
RewriteRule ^dir/page1$ /bleh/docs/?id=12 [L,QSA]
RewriteRule ^dir/page2$ /bleh/docs/?id=13 [L,QSA]
RewriteRule ^dir/page3$ /bleh/docs/?id=14 [L,QSA]
Sometimes one of these rules may be accessed via a redirect from another site (referer). I would like to be able to append the referrer to the query string like this:
RewriteRule ^dir/page2$ /bleh/docs/?id=13&ref=%{HTTP_REFERER} [L,QSA]
However this does not seem to work.
What am I doing wrong?
If you're spelling it HTTP_REFERER (this is unclear due to edits), you're doing it correctly. If it isn't working at that point, it's because the referer isn't being supplied. (Which there are any number of reasons for; supplying it is at the browser's discretion.)
The variable for the referrer uses the common misspelling: HTTP_REFERER. See this cheat-sheet for some more variable names.
As far as I know, a rewritten URL won't remove the original Referer header though, so you should still be able to fetch it from your code without passing it as a query string parameter (provided it gives you access to the HTTP variables.)
A tested method that works for me and transfers the refferer through a 301 redirect.
https://webmasters.stackexchange.com/questions/4665/

Resources