htaccess wildcard subdomain rewrite without infinate loop - php

I want anything.domain.co.uk to go to a php script which then uses the 'anything' from the url within the html to create different headers for example.
This is the script in my .php file for this
<?php echo array_shift(explode(".",$_SERVER['HTTP_HOST']));?>
I have used the script below but it creates a loop obviously.
How do I get anything.domain.co.uk to pick up the php script, still have anything.domain.co.uk in the url and also have www.domain.co.uk go to 'normal' index page
Options +FollowSymlinks -MultiViews
RewriteEngine on
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !www.domain.co.uk$ [NC]
RewriteCond %{HTTP_HOST} ^(www.)?([a-z0-9-]+).domain.co.uk [NC]
RewriteRule (.*) http://%2.domain.co.uk/$1 [R=301,L]

You can use the Location directive to locate the script you want to target and the use an Allow from to limit access to certain domains:
<Location /script.php>
Order Deny,Allow
Deny from all
Allow from anything.domain.co.uk
</Location>

Related

htaccess - Redirect to error page if user attempts direct file access?

I have a website where the main directory contains a few files i don't want to be directly accessed using urls like this:
http://website.com/somefile1.ext1
http://website.com/somefile2.ext1
http://website.com/somefile1.ext2
http://website.com/somefile2.ext2
Currently they download if you enter the urls in a browser. Instead i would like to redirect them to a error page like this:
http://website.com/404
I found this snippet online but it doesn't work, it also doesn't redirect:
<Files ~ "\.(ext1)$">
Order allow,deny
Deny from all
</Files>
What would the correct .htaccess code be to do this task?
Edit:
I redirect site.com/script.php etc to site.com/script with this code:
RewriteEngine On
# turn on the mod_rewrite engine
RewriteCond %{REQUEST_FILENAME}.php -f
# IF the request filename with .php extension is a file which exists
RewriteCond %{REQUEST_URI} !/$
# AND the request is not for a directory
RewriteRule (.*) $1\.php [L]
# redirect to the php script with the requested filename
Try it like this I didn't tried it for now,
RewriteEngine On
RewriteCond %{REQUEST_URI} \.ext\d$
RewriteRule ^ 404 [R=301]
Try
<FilesMatch "somefile\.ext$">
Deny from all
</FilesMatch>
You don't need to use the Order directive if you want to deny access from all.

htaccess to allow only visits from ip but whitelist a dir and a file?

I want to add a htaccess to allow visits from a specific ip.
The tree goes like this
domain
/abc/
/def/
I want to restrict the folder /abc/ but whitelist the folder /def/
Also, on the /abc/ there is a specific file called ghi.php. Can I allow access to that specific file ?
How can I do this?
This is what i have in /abc/ that redirects everyone who is not into the specified ip. However, I want to allow access to the ghi.php inside that dir.
RewriteEngine on
RewriteCond %{REMOTE_ADDR} !^125\.17\.119\.16$
RewriteRule ^ http://www.domain.com/ [R]
I would not use mod_rewrite for content protection, use the modules that are created for this:
# Default: deny all
Order Allow,Deny
# Allow the IP for everything
Allow from 125.17.119.16
# Allow access to this one PHP file
<Files /abc/ghi.php>
Allow from all
</Files>
# Allow access to everything inside that folder
<FilesMatch "^/def/">
Allow from all
</FilesMatch>
Try :
RewriteEngine on
#Allow access to the file
RewriteRule ^abc/ghi\.php$ - [L]
#forbid the request for /abc/* if ip not=1.2.3.4.5
RewriteCond %{REMOTE_ADDR} !^1\.2\.3\.4\.5$
RewriteRule ^abc - [F,L]
If you want to redirect the request to homepage, just change the line
RewriteRule ^abc - [F,L]
to
RewriteRule ^abc http://example.com/ [L,R]

.htaccess with multiple rules

I want to create an htaccess file with the following rules:
I want to redirect:
http://computingessentials.tk/episodes/6
To:
http://computingessentials.tk/episodes.php?id=6
Same applies with the remaining episode.php ids
Next, I want to redirect:
http://computingessentials.tk/episode.php?id=56
To:
http://computingessentials.tk/episode/56
Same applies with the remaining episode.php ids
And last but not the least, I want to remove all the .php extensions of the files.
Since I am farlynew to htaccess i don't really know how to get all of these in an htaccess.
Help would be very much appreciated.
Make sure your mod rewrite of apache is on and put these rules in .htaccess
RewriteEngine on
RewriteRule ^episodes/([0-9]+)$ episodes.php?id=$1 [L]
You can use these 2 rules in your root .htaccess:
Options +FollowSymLinks -MultiViews
RewriteEngine on
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+episodes\.php\?id=([^\s&]+) [NC]
RewriteRule ^ /episodes/%1? [R=301,L]
# internal forward from pretty URL to actual one
RewriteRule ^episodes/([^/.]+)/?$ /episodes.php?id=$1 [L,QSA,NC]

.htaccess RewriteEngine with with various query strings

I have a site which can be open with multible get Resquests and without one.
So there is
?group=x , ?id=x, ?ha=x
All of them can be used at the same time, or only 1 or 2 of them.
I want to rewrite my http://www.example.com/test.php?....
to http://www.example.com/example
EXAMPLE:
So when i open www.example.com/test.php?group=x&id=y it should be displayed as www.example.com/example
And when i open www.example.com/test.php?ha=z
it should also display www.example.com/example
Is this possible with the mod_rewirte?
You could try putting a rule in your .htaccess file like this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^test.php.*$ [NC]
RewriteRule ^test.php.*$ example [L]
</IfModule>
It should only apply to requests that start with /test.php and redirect all of them to http://wwww.yoursite.com/example .
You are going to lose your query string parameters with this method, unless you pass them using clean URLs, which you have to configure with htaccess as well.

Using rewrite mod in .htaccess

I'd like to write a rewrite rule to do the following: if any of the following files exist on the server as a static html file in a specific directory then I'd like .htaccess to serve that static file. Otherwise, I'd like the id (first number after the .com/ and before the first hyphen) to be passed as a query parameter to www.gallery.com/index.php
Redirect should occur for the following URLs if it doesn't exist as a static HTML page.
www.gallery.com/2-swimming.html
www.gallery.com/2
gallery.com/2
Below is my entire .htaccess file
<Files .htaccess>
order allow,deny
deny from all
</Files>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENANE} !-f
RewriteCond %{REQUEST_FILENANE} !-d
RewriteRule ^([0-9]+)-(.*)\.html$ /index.php?id=$1 [L]
</IfModule>
Is there anything wrong with my rewrite condition.(I'm having a hard time). Also what is an efficient way to grab the id incase of a redirect.
Why not just always pass everything to index.php?
PHP is a lot better at manipulating strings than .htaccess
The -f line already takes care of existing static files. (And -d for directories)
RewriteRule ^(.*)$ /index.php?id=$1 [L,QSA]
This rule matches and captures everything.

Categories