I'm trying to setup a redirect for a site.
I want rewrite file requests from a folder to a subdomain url.
At the moment, I have this in my htaccess:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_METHOD} =GET
RewriteCond %{HTTP_HOST} !^example.com
RewriteCond %{HTTP_HOST} ^g.example.com
RewriteRule ^(uploads/(.*)+ uploads/%1%{REQUEST_URI} [L]
The idea is that when a visitor requests:
http://g.example.com/uploads/somefile.png
they are served:
http://example.com/uploads/somefile.png
So it's working as expected. However the problem is that:
http://b.example.com/uploads/somefile.png
also works.
I only want http://g.example.com/ to be rewritten. http://b.example.com/ should 404.
This should work:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^g.example.com$ [NC]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(.*)\.(JPG|GIF|PNG) [NC]
RewriteRule ^ - [R=404,L]
RewriteCond %{HTTP_HOST} ^g.example.com$ [NC]
RewriteCond %{THE_REQUEST} !^GET [NC]
RewriteRule ^ - [R=404,L]
The first rule will make sure that any URL ending with JPG, GIF, PNG that does not come from g.example.com send a 404 not found request to the user.
The second rule makes sure that g.example.com are only accessed using GET requests.
Related
I need to make a 301 redirect from
https://www.example.net/sub1/specific_keyword/page1/page2
https://www.example.net/sub2/sub3/specific_keyword/page3/page4
https://www.example.net/specific_keyword/page5/page6
to
https://www.example.net/sub1/
https://www.example.net/sub2/sub3/
https://www.example.net/
I tried this code:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.net$ [NC]
RewriteRule ^(.*)\/specific_keyword$ "https\:\/\/www\.example\.net\/$1" [R=301,L]
But no luck.
With your RewriteRule attempt, you demanded that the requested URL
ends with /folder-to-remove, via the $ at the end, so that won’t match (Source: #comment120998408_68464303)
With that fixed, place following rules at top of your htaccess Rules file. Make sure to clear your browser cache before testing your URLs or use a redirect checker online.
RewriteEngine ON
RewriteCond %{HTTP_HOST} ^www\.example\.net$ [NC]
RewriteRule ^([^/]*)/([^/]*)/.*$ $1/$2? [R=301,NE,L]
OR only match the specific keyword
RewriteEngine ON
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule ^(.*)specific_keyword/ https://www.example.com/$1 [R=301,L,NE]
I know that there exists several posts asking about the exact same thing. I asked the same question again, since I've read each and every one of them, and tried the solutions. Maybe they worked for the O.Ps'es codes, but unfortunately didn't work for mine.
I really need to disable HTTPS on a single PHP page called play.php, so that the page is accessible via direct HTTP, or redirect to HTTP if directly requested via HTTPS.
I need to change https://example.com/play/blabla to http://example.com/play/blabla, while the rest of the site is forced HTTPS.
Here is my full .htaccess code:-
Header set Access-Control-Allow-Origin "*********"
ErrorDocument 404 /pagenotfound.php
ErrorDocument 403 /pagenotfound.php
Options -MultiViews
RewriteEngine on
RewriteBase /
Options +FollowSymLinks
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
RewriteRule ^search/(.*)$ viewgames.php?search=$1 [L,QSA,NC]
RewriteRule ^category/(.*)$ viewgames.php?cat=$1 [L,QSA,NC]
RewriteRule ^users/(.*)$ users.php?action=$1 [L,QSA,NC]
RewriteRule ^play/(.*)/$ play.php?gn=$1 [L,QSA,NC]
RewriteRule ^play/(.*)$ play.php?gn=$1 [L,QSA,NC]
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^([a-z]+)/?$ $1.php [L,NC]
I have a script called play.php which is rewrited using .htaccess to /play/ i.e if someone requests example.com/play/foobar, the request will be sent to example.com/play.php?gn=foobar.
I'm very new to .htaccess. I will really appreciate your help.
[Edit] I have updated the above code to show my full .htaccess. I hope it helps. BTW, the stars (*) on the first line of code is to hide my actual website address.
Here is full .htaccess with my comments:
Header set Access-Control-Allow-Origin "*********"
ErrorDocument 404 /pagenotfound.php
ErrorDocument 403 /pagenotfound.php
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteBase /
# it is important to keep www removal rule as first rule to avoid multiple redirects
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L,NE]
# http->https if URL is not starting with /play/ or /play.php
RewriteCond %{HTTPS} !on
RewriteRule !^play(?:\.php|/.*)?$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE,NC]
# https->http if URL is starting with /play/
RewriteCond %{HTTPS} on
RewriteRule ^play(?:/.*)?$ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE,NC]
RewriteRule ^search/(.*)$ viewgames.php?search=$1 [L,QSA,NC]
RewriteRule ^category/(.*)$ viewgames.php?cat=$1 [L,QSA,NC]
RewriteRule ^users/(.*)$ users.php?action=$1 [L,QSA,NC]
RewriteRule ^play/(.+?)/?$ play.php?gn=$1 [L,QSA,NC]
# make sure to check for presence of .php file before rewrite
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([a-z]+)/?$ $1.php [L,NC]
It is important to completely clear browser cache or use a new browser of testing the changes.
You can disable https to your play.php URI using the following RewriteRule
RewriteEngine on
RewriteCond ℅{REQUEST_URI} play\.php$ [OR]
RewriteCond ℅{REQUEST_URI} /play
RewriteCond ℅{HTTPS} on
RewriteRule ^ http://℅{HTTP_HOST}℅{REQUEST_URI} [L,NE,R]
This will redirect https://example.com/play.php to its non SSL version.
Since your rule already checks http urls , you can also add a condition to your rule to exclude the play.php from HTTPS redirection something like the following :
RewriteCond %{HTTPS} off
#redirect all to https except /play and /play.php
RewriteCond %{REQUEST_URI} !play
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
I have an .htaccess file on an old domain of mine that includes redirects that aren't redirecting as I am wanting.
If I type in olddomain.com/about-us.html the redirect is sending the user to newdomain.com/about-us.html. What I am trying to do is direct the user to newdomain.com/about.
Below is my .htaccess file with one example of the redirect.
Does anyone see what I am doing wrong?
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?olddomain\.com$ [NC]
RewriteCond %{THE_REQUEST} !/administrator [NC]
RewriteRule /about-us.html https://newdomain.com/about [L,R=301]
Redirect 301 /about-us.html https://newdomain.com/about
RewriteRule ^ https://newdomain.com%{REQUEST_URI} [L,R=301,NE]
Network Tab:
Have it like this:
RewriteEngine On
RewriteRule ^about-us\.html$ https://newdomain.com/about [L,R=301,NC]
RewriteCond %{HTTP_HOST} ^(www\.)?olddomain\.com$ [NC]
RewriteCond %{THE_REQUEST} !/administrator [NC]
RewriteRule ^ https://newdomain.com%{REQUEST_URI} [L,R=301,NE]
Make sure to test it in a new browser or completely clear browser cache.
Make sure there is no other code in your .htaccess when you test
I have a https website which has a hidden user area. From there the users has a link which links to a http site. With a referrer the site makes sure that the userers are coming from my site. Other access attemps are blocked.
Since my webstie change to https, there referrer is not working anymore.
My solution attempt so far:
Inside my htaccess I want to stop the https for only one file on the site. Where the link is. My htaccess so far.
ErrorDocument 404 /404.php
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} !^/Mitgliederservice/Infoline/index\.php$ [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
I don't understand whether you want to exclude one page or force only one page to https so , if you want to exclude one specific page use the following code :
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteCond %{REQUEST_URI} !^/path/to/yourpage\.php$
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R]
otherwise use the following code :
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteCond %{REQUEST_URI} ^/path/to/yourpage\.php$
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R]
I've managed to have .htaccess redirect some of my domains to subdomains. Now i'd like to change the title of the urls. Or to put it differently, change how the url appears on screen in the url input of the browser, but i just can't seem to get it to work. I must've overlooked something.
I'd like it to be so that it won't show http://subdomain.domain.com/, but just http://www.subdomain.com/. And I do own the necessary domains.
My current code:
RewriteEngine On
Options +FollowSymlinks
RewriteCond %{HTTP_HOST} ^(www\.)?sub1\.com$ [NC]
RewriteRule ^ http://sub1.domain.com [R,L]
RewriteCond %{HTTP_HOST} ^(www\.)?sub2\.com$ [NC]
RewriteRule ^ http://sub2.domain.com [R,L]
Any suggestions?
Try this
RewriteCond %{HTTP_HOST} ^sub1\.domain\.com$
RewriteRule ^/(.*) http://sub1.com/$1 [redirect,last]
RewriteCond %{HTTP_HOST} ^sub2\.domain\.com$
RewriteRule ^/(.*) http://sub2.com/$1 [redirect,last]