Need to use httpd.conf instead of .htaccess for rewrite - .htaccess

I am working on a host that blocks my .htaccess file from being used so I can not change my permalinks using it. I need to figure out what code to use and where to put it in my httpd.conf file to get the same effect.
The code in the .htaccess file is below:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /wordpress/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /wordpress/index.php [L]
</IfModule>

You'll need to wrap that code in a <Directory> directive. Where it goes will depend on what else you've got in your http.conf file. See the Apache docs for more info.
However, as blockhead says; if your host won't let you use .htaccess files, you've got virtually no chance of being allowed near the httpd.conf file.

For example, if you'd like to block access to GoogleBot throughout your entire server (which may be comprised of hundreds of virtual hosts), you can add this to your httpd.conf file:
#setup the root dir
<Directory />
RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} Googlebot
RewriteRule .* - [F,L]
</Directory>
This will send a HTTP 403 Forbidden to anyone who comes in with Googlebot in their user agent string. And this rewrite condition will be applied to ALL virtual hosts, by virtue of applying this to the "/" folder.

Related

url redirection in the default .htaccess file

I would like to map "http://www.example.com/abc" to "http://www.example.com/test/abc" for having the shortest route possible. I am using pyroCMS for my users and content.
the default pyrocms file:
# Multiple Environment config
# Set this to development, staging or production
# SetEnv PYRO_ENV production
<IfModule mod_rewrite.c>
# Make sure directory listing is disabled
Options +FollowSymLinks -Indexes
# disable the Apache MultiViews directive if it is enabled on the server. It plays havoc with URL rewriting
Options -MultiViews
RewriteEngine on
# Keep people out of codeigniter directory and Git/Mercurial data
RedirectMatch 403 ^/.*/(vendor|composer\.json|composer\.lock|system/cms/cache|system/codeigniter|system/cms/config|system/cms/logs|\.git|\.hg).*$
# Send request via index.php (again, not if its a real file or folder)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
<IfModule mod_php5.c>
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
<IfModule !mod_php5.c>
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
</IfModule>
I would like to add this rule to the file:
RewriteRule ^([a-z0-9_]+)$ test/$1
To rewrite "BASE_URL/abc" to "BASE_URL/test/abc"
However,
i tried many positions as to where to put this RewriteRule, my website keeps giving a "Page Missing".
Is my RewriteRule ok? And where do i insert it?
PyroCMS has built-in modular routing ability. see here:
http://docs.pyrocms.com/2.2/manual/developers/basics/modular-routing
If your "http://www.example.com/abc" refers to a custom module,then, you can add a file named "routes.php" in a in config folder of your module.
the folder construction should like this :
addones/shared_addons/modules/your-module/config/routes.php
OR even you can edit the core route config file located at system/cms/config/routes.php and add this line or whatever your routing rules are:
$route['abd'] = 'test/abd';
OR more even, at your control pannel there is a redirect module that you can add redirections.
It all depends on what your rule is supposed to do and how it is supposed to interact (or not interact) with the rest of your site. And considering your entire htaccess file is mostly commented out code (which I removed to make it halfway readable), you just want to place it under RewriteEngine On.
However, since it blindly routes everything into test you need to add a few conditions and make it something lilke:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/test%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}/test%{REQUEST_URI} -d
RewriteRule ^([a-z0-9_]+)$ test/$1 [L]

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.

Basis mod_rewrite rule results in 500 internal server error

I have this very basic rewrite rule, no matter what I try, results in an Error 500.
RewriteEngine On
RewriteRule ^folder/(.*) /folder/index.php?Alias=$1 [L]
My httpd.conf file has the following content: (which seems OK to me)
<Directory "/var/www/html">
Options -Indexes FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
<IfModule mod_suphp.c>
suPHP_Engine On
suPHP_UserGroup webapps webapps
SetEnv PHP_INI_SCAN_DIR
</IfModule>
</Directory>
Any suggestions on what might be going wrong? I've also tried to add $ at the end of my rewrite rule.
The rewrite engine will loop repeatedly, until the URI stops changing, or the internal redirect limit is reached which causes the 500 error to be thrown. Your rule's target URI /folder/index.php will get thrown back into the rewrite engine and your same rule's regex matches it, ^folder/(.*). So you'll need to add some kind of condition to prevent the loop.
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/folder/index\.php
RewriteRule ^folder/(.*) /folder/index.php?Alias=$1 [L]
This is simple, it simply won't apply the rule if it already starts with /folder/index\.php. You can also try:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^folder/(.*) /folder/index.php?Alias=$1 [L]
This is a little less restrictive of a condition. It only applies the rule if the requested URI doesn't map to an existing file or directory. This assumes that when you try to go to /folder/blahblah there isn't a directory or file blahblah and that you want to route it through your index.php.

Getting rid of index.php in codeigniter with Vhost

I've tried every possible htaccess snippet I could find about this but none will work. My codeigniter projects is found on ci.mydomain.com and I've configured the virtualhost this way
<VirtualHost *:80>
ServerName ci.mydomain.com
ServerAlias ci.mydomain.com
ServerAdmin x#gmail.com
DocumentRoot /var/www/codeigniter
</VirtualHost>
I've also set
$config['index_page'] = '';
I just get 404. What's wrong?
I'm using the latest version 2.1.3
Update
<VirtualHost *:80>
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [PT]
ServerName ci.domain.com
ServerAlias ci.domain.com
ServerAdmin d#d.com
DocumentRoot /var/www/codeigniter
</VirtualHost>
I tried this and it worked for me. Although I would rather use a .htaccess file.
Edit: Guys, the reason I mentioned that I use virtual host is that i suspect that is what causes the problem. But maybe I'm wrong.
The actual workhorse for this is a module named mod_rewrite, it allows you to set up rewrite rules to redirect urls to index.php for processing.
See this article for some more in depth details on how to go about this
Update:
It looks like the rewrite conditions you're using are sort of following the old rules.
The two things I think you're missing (one of which isn't relevant, but helpfull) are:
RewriteBase - I have found specifying a rewrite base goes a long way to helping your rules work better. Technically I think it should work without but I've encountered nothing but problems without it.
RewriteCond Statements for Excluding actual files - Right now your conditions will mask a lot of real files, which probably isn't what you want.
So how do we fix this?
RewriteBase /
This will set the root of your rewrite engine to start after the .com/ this simplifies things because you wont need to deal with the leading /.
As a result you'll need to change your main rewrite rule to this:
RewriteRule ^(.*)$ index.php/$1 [L]
This will redirect .com/anything/you/type to .com/index.php/anything/you/type and the php script can pick that up automatically to pass to the router.
This relys on the condition however because otherwise you'd end up in a loop, so above this line you need these conditions to prevent actual files/folders from being hidden:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
With this !-f means "Not -f" where -f means "Is A File", the same for -d meaning "Is a Directory" so basically you're saying match this rule only if the %{REQUEST_FILENAME} is not a file and is not a directory.
Then any url that points to an actual file will resolve just fine! (Including index.php).
So with the following lines in your .htaccess you should get proper rewriting:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
You should be good to go provided you have mod_rewrite enabled in your apache configuration.
If you're using Apache2 on Ubuntu, the command(s) to enable this is trivial:
a2enmod rewrite
service apache2 reload
It's important the module is actually enabled of all your Rewrite* rules in your apache will not execute.
If that still isn't working you need to go check the apache main configuration to see if there's a subset of commands allowed in the .htaccess files, usually an install is set to All so that anything can be specified there, but often it's locked down to prevent security overlap between customers. You mentioned you're using a VPS so you probably wont have to do this step, but I thought I would mention it for the sake of others.
The option you'd be looking for looks like this:
AllowOverride All
There is one last thing that's mainly a saftey net:
Apache offers a way to gaurd clauses against modules existing
<IfModule mod_rewrite.c>
~~ Put Rewrite* Rules here ~~
</IfModule>
Update 2
Ok I just noticed the Virtual host. I think the problem is you're putting the Rewrite conditions in your <VirtualHost > block, and they need to exist in the <Directory> block for the folder your files are in.
You can do this by removing all Rewrite* rules from your conf and either adding this to your .conf file:
<Directory /var/www/codeigniter>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</Directory>
Or adding a .htaccess file in /var/www/codeigniter with this inside of it:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
These rules apply to the directory, not the VHost declaration, and the .htaccess is a shortcut to the <Directory> block basically.
Useful Links:
Apache mod_rewrite documentation

htaccess rewriterule ignore index.html

I'm using CodeIgniter and .htaccess to rewrite the URLs but I want it to ignore index.html.
This is so we can upload an index.html at any time as this will be a temporary landing page. With a link through to the main site on the index.php.
This is what is currently in .htaccess and I have set the server's directory index to index.html index.php
RewriteEngine on
RewriteCond $1 !^(index\.html|index\.php|js|img|fonts|data|css|uploaded|mobile_devices|audioplayer|emails|robots\.txt|archive_blog)
RewriteRule ^(.*)$ /index.php/$1 [L]
Thanks for any help
that rather looks like you want to rewrite everything that does not really exist in your directory.
try this instead of your current RewriteCond:
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1
Instead of using RewriteCond to ignore the index.html file, you could instead restrict access to it directly through using the FilesMatch directive. FilesMatch accepts a regular expression which could filter based on file name (e.g., index.html) or any regular expression.
Blocking access to the index.html file
<FilesMatch "index\.html$">
Order allow,deny
</FilesMatch>
That would completely deny access to the index.html file. I will admit, that I do not know the negative effects this would have on search engine crawling.
To read more about the FilesMatch Directive see http://httpd.apache.org/docs/current/mod/core.html#filesmatch
As for the rest of the directories in that list you currently have, you could just lock down all directory access regardless of name. It would give you a little more coverage going forward.
Options -Indexes
To read more about the Options Directive see http://httpd.apache.org/docs/current/mod/core.html#options
In the end your new .htaccess file would look something like this:
# Protect specific files from access
<FilesMatch "index\.html$">
Order allow,deny
</FilesMatch>
# Hide directory listing for URL's mapping to a directory
Options -Indexes
# Follow all symbolic links in this directory
Options +FollowSymLinks
# General rewrite rules
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
</IfModule>
Why don't you use the built in environment function?
You could create a 'maintenance' environment and set the base controller to whatever you want. Then you would just need to edit the index.php file to specify the environment you want.

Resources