Mod-Rewrite in .htaccess? - .htaccess

I'd like to implement 301 redirection from http://www.onbip.com/index-en.html to http://www.onbip.com/
In htaccess file I have:
RewriteRule ^.htaccess$ - [F,L] #403 Forbidden
RewriteRule ^inc/ - [F,L]
RewriteCond %{HTTP_HOST} ^onbip\.com
RewriteRule ^(.*)$ http: //www.onbip.com/$1 [R=permanent,L]
RewriteRule ^index-([^\.]+)\.html$ index.php?lang=$1 [L]
I Need to standardize the default page which will be http://www.onbip.com/
How?

In your httpd.conf file, there should already be a line to forbid access to .ht* files that will probably look like this:
<Files ~ "^\.ht">
Order allow,deny
Deny from all
Satisfy All
</Files>
If you want to be redundent, using Files or FilesMatch to protect it would probably be good. If you want to use Rewrite for this, you could throw a 404 as though it doesn't exist.
Here is a redirect (not a mod_rewrite) for a directory /inc to a 404 page
This is at http://httpd.apache.org/docs/2.0/mod/mod_alias.html
Redirect 404 /inc
Now for rewrite
see http://httpd.apache.org/docs/current/mod/mod_rewrite.html
#Set the page (and order of if they are there) to be shown if asked for a directory
#just put index.php if that's all you want
DirectoryIndex index.php index.html
RewriteEngine on
# if not www.onbip.com, then send to http://www.onbip.com
RewriteCond %{HTTP_HOST} !^www\.onbip\.com [NC]
RewriteRule ^(.*)$ http://www.onbip.com/$1 [R=301,NC,L]
# Now if entered "/index-ab.html" then call "/?lang=ab"
# You might want to see about the regex for proper lang, I put something like "en" or "us-en"
RewriteBase /
RewriteRule ^index-([a-z]{2}(-[a-z]{2})?)\.html$ ?lang=$1 [R=301,NC,L]
The last will call "/" from the server which will be "index.php" first if it exists according to directory index.

Related

Htaccess, go through files until exists?

I have this .htaccess file:
Options +FollowSymLinks
Options -Indexes
RewriteEngine On
RewriteEngine On
RewriteRule \.(png|jpg|gif|jpeg|bmp|ico|flv|mpeg|mp4|mp3|swf|exe|WAgame|wsc|eot|svg|ttf|woff|woff2|rar|wav)$ - []
RewriteRule ^ entryPoint3.php
RewriteRule ^ entryPoint2.php
RewriteRule ^ entryPoint.php
RewriteRule ^robots\.txt$ http://www.example.com [R,NE,L]
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST} [L,R=301]
<Files .htaccess>
Order Allow,Deny
Deny from All
</Files>
If the entryPoint3.php exists, load it, otherwise load entryPoint2.php otherwise entryPoint.php. How to achieve this?
Have it this way:
Options +FollowSymLinks -Indexes
RewriteEngine On
RewriteRule \.(png|jpg|gif|jpeg|bmp|ico|flv|mpeg|mp4|mp3|swf|exe|WAgame|wsc|eot|svg|ttf|woff|woff2|rar|wav)$ - [L]
RewriteRule ^robots\.txt$ / [R,NC,L]
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
# if entryPoint3 exists then use it
RewriteCond %{DOCUMENT_ROOT}/entryPoint3.php -f
RewriteRule ^ entryPoint3.php [L]
# if entryPoint2 exists then use it
RewriteCond %{DOCUMENT_ROOT}/entryPoint2.php -f
RewriteRule ^ entryPoint2.php [L]
# if entryPoint exists then use it
RewriteCond %{DOCUMENT_ROOT}/entryPoint.php -f
RewriteRule ^ entryPoint.php [L]
<Files .htaccess>
Order Allow,Deny
Deny from All
</Files>
I solved it like that: DirectoryIndex entryPoint3.php entryPoint2.php entryPoint.php what's the difference?
...but remember it will try to open entryPoint files in every directory not just in site root
You can specify root-relative URL-paths as arguments to the DirectoryIndex directive, they do not need to be relative, as you have here. For example:
DirectoryIndex /entryPoint3.php /entryPoint2.php /entryPoint.php
This will search for files in the document root only, regardless of which directory is requested.
But also, this is quite different to #anubhava's solution using mod_rewrite (and similar to what you were attempting in the question). With DirectoryIndex, the necessary file will only be served if you are requesting a directory (which includes the document root, ie. https://example.com/). eg. example.com/ and example.com/directory/ will trigger the relevant entryPoint file, but example.com/something will not.
However, with the mod_rewrite solution as stated, the relevant entryPoint file is served for "any" requested URL (except for the few URL-extension exceptions as stated). eg. example.com/, example.com/something, example.com/file.php and example.com/directory/ will all trigger the relevant entryPoint file.
For the mod_rewrite solution to work the same way as DirectoryIndex you will need an additional condition that explicitly checks that the request maps to a directory (including the trailing slash). For example:
# When requesting a directing... if entryPoint3 exists (in the root) then use it
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{DOCUMENT_ROOT}/entryPoint3.php -f
RewriteRule ^(.+/)?$ entryPoint3.php [L]
:
The RewriteRule pattern simply checks that the requested URL ends with a slash (or is the document root) - the same behaviour as DirectoryIndex. (If you request a directory without a trailing slash then mod_dir appends it with a 301 redirect.)
So the solution you choose really depends on your requirements. However, if DirectoryIndex is working for you then I assume you are only requesting filesystem directories, so DirectoryIndex would be the way to go.

.htaccess pretty url with and without parameter and slash

I have below code in my .htaccess file. When I open https://example.com/tran it shows 404 page but adding slash in url like https://example.com/tran/ works.
How can I make https://example.com/tran to redirect to https://example.com/tran/ and https://example.com/tran/parameter1 both work because also want to get parameter from url using $_GET method when it is available in url.
Options All -Indexes
RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
<FilesMatch ".(htaccess|htpasswd|ini|phps|fla|psd|log|sh)$">
Order Allow,Deny
Deny from all
</FilesMatch>
RewriteRule tran/(.*) transaction.php?id=$1
RewriteRule tran/(.*)/ transaction.php?id=$1
RewriteRule addr/(.*) address.php?id=$1
RewriteRule addr/(.*)/ address.php?id=$1
You can use:
RewriteRule tran(?:/(.*?)/?)?$ transaction.php?id=$1
RewriteRule addr(?:/(.*?)/?)?$ address.php?id=$1
instead of your 4 rules

.htaccess specific url changes

I've a small, but hard to understand problem with .htaccess in CMS system.
I've mod expires, that cache stuff on whole website, but I don't want to cache stuff in /admin URL, I can't make another .htacess, couse I've MVC structure and no real directory that could hold all my admin stuff.
I've found directive, but it only works in server configuration and I want it to work on different hostings, so only in htaccess file.
EDIT- Rewrite
# Turn on URL rewriting
RewriteEngine On
# Installation directory
RewriteBase /
RewriteCond %{HTTP_HOST} !^www. [NC]
RewriteCond %{HTTP_HOST} ![0-9]$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=301]
# Protect hidden files from being viewed
<Files .*>
Order Deny,Allow
Deny From All
</Files>
# Protect application and system files from being viewed
RewriteRule ^(?:application|modules|system)\b.* index.php/$0 [L]
# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite all other URLs to index.php/URL
RewriteRule .* index.php/$0 [PT]
You can apply your Expires directive using a <if> directive with an expression to match against /admin:
<If "%{REQUEST_URI} =~ /^\/admin\//">
# Your expiry directives
</If>
If you know the exact URL then you can try this pattern.
RewriteRule ^facebook/get/(.*)?$ http://$1 [NC,R]
RewriteRule ^wrapper/share/(.*)?$ http://example.com/wrapper/share/$1 [NC,R]
This will check for URL where <-any-value->facebook/get/<-any-value2-> and then will send to the <-any-value2->
Like
RewriteRule ^stats/(.*)$ admin/dashboard.php?mode=openstats&event_id=$1 [NC,L,QSA]
**If URL has stats/<--any-value--> then it will redirect/open admin/dashboard.php **
If your URLs doesn't have exact value but you do know the URL slot pattern then you can try this.
RewriteRule ^([^/.]+)/([a-zA-Z0-9_-]+)/$ wrapper/index.php?id=$2 [NC,L,QSA]

301 redirect everything to new website root/whole site including sub pages? Moved Permanently

QUICK UPDATE
Ok getting there
Is this mod_alias?
RedirectMatch 301 ^/ http://brightmist.co.uk/
I've added this one line of code underneath everything and it appears to work, however my other directories such as http://brightmist.co.uk/blog/2013/02/23/manchester-art-gallery-feb-2013 are telling google these pages have temporarily moved - see http://www.internetofficer.com/seo-tool/redirect-check/
Does this mean I have to go right the way though my site and add a tone of redirects?
ORIGINAL QUESTION
I have a new website.
I'd like to redirect all of my old site links from http://artygirl.co.uk to my new one http://brightmist.co.uk/
I'm primarily a designer with years of experience using mainly in Photoshop, CSS, HTML, Wordpress, and jQuery but I don't know much about editing things like the htaccess file. And I don't want to get it wrong as it means google ranking drops etc
Does anyone know of any script I can paste into the bottom of my htaccess file, I'd like it to redirect all links/pages on the site to the same place as before. For example if I type http://artygirl.co.uk/buy-art-prints-cheshire/ I want it to go to http://brightmist.co.uk/buy-art-prints-cheshire/ I'm using the same host, they've just re-pointed the domain
Among other things my host has recently added the following code, I assume this is also to do with the domain mapping, also here is my whole htaccess file -
ErrorDocument 401 /forms/401.html
ErrorDocument 403 /forms/403.html
RewriteEngine On
RewriteBase /
#uploaded files
RewriteRule ^(.*/)?files/$ index.php [L]
RewriteCond %{REQUEST_URI} !.*wp-content/plugins.*
RewriteRule ^(.*/)?files/(.*) wp-includes/ms-files.php?file=$2 [L]
# add a trailing slash to /wp-admin
RewriteCond %{REQUEST_URI} ^.*/wp-admin$
RewriteRule ^(.+)$ $1/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule . - [L]
RewriteRule ^([_0-9a-zA-Z-]+/)?(wp-.*) $2 [L]
RewriteRule ^([_0-9a-zA-Z-]+/)?(.*\.php)$ $2 [L]
RewriteRule . index.php [L]
<IfModule mod_security.c>
<Files async-upload.php>
SecFilterEngine Off
SecFilterScanPOST Off
</Files>
</IfModule>
<Files 403.shtml>
order allow,deny
allow from all
</Files>
deny from 218.143.4.127
deny from 143.90.221.204
deny from 95.135.78.190
deny from 114.108.150.74
deny from 95.135.111.205
deny from 91.124.239.150
deny from 94.178.2.93
deny from 91.124.206.118
deny from 91.124.226.116
deny from 118.98.32.34
deny from 94.180.252.133
deny from 58.27.140.58
deny from 77.93.197.83
deny from 88.191.63.27
# Hotlink Protection START #
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?brightmist.co.uk [NC]
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?google.com [NC]
RewriteRule \.(jpg|jpeg|png|gif)$ - [NC,F,L]
# Hotlink Protection END #
I'd like to redirect all of my old site links from http://artygirl.co.uk/ to my new one http://brightmist.co.uk/
There are several ways to achieve that, all of them should be implemented in one .htaccess file in http://artygirl.co.uk/ root directory.
There is no need to check for the incoming domain as it must be artygirl.co.uk, where the .htaccess file is located.
To use any of the following options, copy-paste the corresponding directive or rule-set into one empty .htaccess file in http://artygirl.co.uk root directory.
The fastest one is a simple Redirect using one mod_alias directive:
Redirect 301 / http://brightmist.co.uk/
Any path in the incoming URL will be appended automatically to the redirected URL.
To redirect only certain paths using another mod_alias directive:
RedirectMatch 301 ^/(.*) http://brightmist.co.uk/$1
Although this example redirects everything, the regex ^/(.*) can be modified to match only certain URL-path pattern.
To redirect only certain paths using mod_rewrite directives:
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) http://brightmist.co.uk/$1 [R=301,NC,QSA,L]
As in the previous option, although this rule-set redirects everything, the regex ^(.*) can be modified to match only certain URL-path pattern.
NOTES
The same directory structure and files in http://artygirl.co.uk/ must exist in http://brightmist.co.uk/ for any of the previous options to work.
If the actual .htaccess file in your question works as expected, you could use it in http://brightmist.co.uk/ root directory where the new WP is installed. Might require some modifications, though.
To move or copy a WP install, check this link Changing the site URL.
UPDATE:
From these sentences in your comment to this answer: "My domains both point at the same directory..." and "...now it creates a loop...", maybe the question is about domains pointing to the same content (Website), normally known as parked domains.
If that's the case, I am not sure redirecting in .htaccess the primary domain to the parked one is the correct approach just to change the domain name in the browser's address bar.
However, in theory something like this should do it using mod_rewrite directives:
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} artygirl\.co\.uk [NC]
RewriteRule ^(.*) http://brightmist.co.uk/$1 [R=301,NC,QSA,L]
Redirects permanently any request from http://artygirl.co.uk to http://brightmist.co.uk, appending the complete incoming path and query when present.
Since the .htaccess file is also shared, I think this rule-set should be placed at the top of the .htaccess file in the question, replacing the following lines:
RewriteEngine On
RewriteBase /
Are they redirecting to the same extension? Should be something like this:
RewriteEngine on
RewriteRule ^(.*)$ http://newdomain.com/$1 [R=301,L]
I see the problem. It redirects the root fine but if I go to:
http://artygirl.co.uk/photography/
I think you want it to go to
http://brightmist.co.uk/photography/
and not
http://brightmist.co.uk/
as it does currently.
Rewrite rules are notoriously hard to debug, but try this...
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^brightmist\.co\.uk$ [NC]
RewriteRule ^(.*)$ http://brightmist.co.uk/$1 [R=301,L]
I know this is really similar to what Matthew Camp answered. The RewriteCond is very important to avoid endless loops because you are delivering both domains from the same folder. You only want to redirect the one that's incorrect.
Rewrite Base might also be required depending on how the servers are configured and what other rewrite rules exist at higher levels (invisible to you but which might still affect you). Try with and without the RewriteBase.
I hope this helps.

Deny direct access to files or directory using .htaccess

I'm playing with .htaccess and I was wondering if with just an .htaccess inside the root directory is possible to block all the request from a browser directed on existing files or directories.
Let's try this example:
RewriteEngine On
RewriteBase /~my_user/my_base/
RewriteRule ^list/$ list.php [L]
RewriteRule ^element_of_list/([a-zA-Z0-9\-]+)/$ element.php?elem_id=$1 [L]
Now, if I write http://127.0.0.1/~my_user/my_base/list/, this is wroking fine but if I write http://127.0.0.1/~my_user/my_base/list.php it's still working. I don't want that. I want the user to obtain a 404 error in the last case.
We have /etc/apache2/mods-enabled/userdir.conf
<IfModule mod_userdir.c>
UserDir public_html
UserDir disabled root
<Directory /home/*/public_html>
AllowOverride All
Options Indexes FollowSymLinks
<Limit GET POST OPTIONS>
Order allow,deny
Allow from all
</Limit>
<LimitExcept GET POST OPTIONS>
Order deny,allow
Deny from all
</LimitExcept>
</Directory>
</IfModule>
My first try was to use RewriteCond:
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*)$ 404.php [L]
But it's not working. Every request ends up redirected to 404.php
UPDATE
So I've managed to create the filter for directories:
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_URI} !^/~my_user/my_base/$ [NC]
RewriteRule ^(.*)$ 404.php [L]
What it does is to check if the requested path (REQUEST_FILENAME) exists and it's a directory AND if it's not my RewriteBase which is basically index.php, then redirect to 404.php
I'm still trying to find something that does the same thing for files. I know I can selectively do that using extensions filename but I want an universal filter for files.
If I've understood your requirements correctly, you're looking to do something like this:
# This is a real directory...
RewriteCond %{REQUEST_FILENAME} -f [OR]
# Or it's a real file...
RewriteCond %{REQUEST_FILENAME} -d
# And it's not 404.php...
RewriteCond $0 !=404.php
# And it's not the root
RewriteCond $0 !=""
# And it's not any of the above due to an internal redirect...
RewriteCond %{ENV:REDIRECT_STATUS} ^$
# So cause a 404 response (you could redirect to 404.php if you want)
RewriteRule ^.*$ - [R=404,L]
# Set the 404 error document
ErrorDocument 404 /~my_user/my_base/404.php
Keep in mind that this blocks everything that exists, so any images, stylesheets, or scripts will be sent to the 404 page too. If you just want to block access to the PHP files, Gumbo's solution is more appropriate. I think in that case you'll need another RewriteCond though to prevent looping:
# Make sure the reason this request has a .php is because it was requested
# by the user (and not due to a redirect)
RewriteCond %{THE_REQUEST} ^[A-Z]+\s/[^\s]+\.php
# Make sure we aren't on 404.php already
RewriteRule %{REQUEST_URI} !404\.php$
# We aren't, so redirect to 404.php
RewriteRule ^ 404.php [L]
Try this rule:
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /[^?\ ]*\.php[/?\ ]
RewriteRule .*\.php$ 404.php [L]
This will rewrite all requests whose paths contain a .php internally to 404.php.

Resources