How to use .htaccess URL masquerading technology - .htaccess

Basically, I was wondering if I could take a url like:
http://tai.tskynet.com/?id=book
and rewrite it to
http://tai.tskynet.com/book/
http://tai.tskynet.com/book
like a "fake" folder.
What should I write?

First, you have to make sure that your rewrite_module and dir_module is enabled in apache. Then check your httpd.conf file to make sure that these lines exist:
LoadModule rewrite_module modules/mod_rewrite.so
LoadModule dir_module modules/mod_dir.so
Then set the value for DirectoryIndex to include the target file (index.php as example)
<IfModule dir_module>
DirectoryIndex index.php ...
</IfModule>
Here is the .htaccess file:
RewriteEngine on
RewriteCond $1 !^(index\.php|robots\.txt|assets|uploads|cloudfront\.php)
RewriteRule ^(.*)$ index.php/$1 [L]
This would forward your URI values to the target file [index.php] then process it there.
In the example, robots\.txt|assets|uploads|cloudfront\.php will not be forwarded to index.php and instead will be treated as normal directory requests.

Related

Restricting RewriteRule to ignore certain folders

I have a basic Rewrite rule defined in the root of my website, in an .htaccess file:
<IfModule mod_rewrite.c>
# http://www.generateit.net/mod-rewrite/index.php
# Tell PHP that the mod_rewrite module is ENABLED.
SetEnv HTTP_MOD_REWRITE On
RewriteEngine on
RewriteRule ^([^/]*)/$ category.php?cat=$1 [L]
RewriteRule ^([^/]*)/([^/]*)/$ post.php?cat=$1&post=$2 [L]
RewriteRule ^blog-([^/]*).php$ default.php?page=$1 [L]
</IfModule>
I have 2 sites hosted on the same folder structure:
DIR: example2.com
file: category.php
file: default.php
file: post.php
So the rewrite rule will redirect e.g.
example.com/post.php?cat=general-ledger&post=gl-journals-volumes
to:
example.com/general-ledger/gl-journals-volumes/
However, I have installed Wordpress in a folder called ddc on example2, but when I visit:
example2.com/ddc/wp-admin/user/
I can't view it, as the system thinks it's trying to view a rewritten page.
Is there any way I can stop the rewrite rule from firing for specific directories?
I have tried:
RewriteCond %{REQUEST_URI} !^/(example2.com|ddc)/?(.*)$ [NC]
But it made no difference. Furthermore, the error log shows this when I try to access the folder:
[Mon Jan 16 22:39:53.365334 2017] [autoindex:error] [pid 28997]
[client my_ip_address_here:53652] AH01276: Cannot serve directory
/home/oracle36/public_html/example2.com/ddc/wp-admin/user/: No
matching DirectoryIndex (default.php) found, and server-generated
directory index forbidden by Options directive
Your rewrite rules are not the problem. They only affect URLs with directories of one or two levels. Your problem is that you don't have index.php set up as a directory index, so there is nothing for Apache to serve from the WordPress directory. Add this to your .htaccess:
DirectoryIndex default.php index.php
I am adding default.php to that because the error message shows you have that set up already, and then adding index.php to it as another option, to support WordPress. You can remove your RewriteCond.

redirect website using htaccess

I have a wesite:
http://site1.com
and a second website:
http://89.89.89.89:8888
I want that when I acces http://site1.com/site2/ to show the content of http://89.89.89.89:8888.
Also I want the URL to remain the same. Ex:
Instead of http://89.89.89.89:8888/Page1.htm i want the URL to be http://site1.com/site2/Page1.htm .
Also when i navigate from Page1.htm to Page2.htm, the URL would be http://site1.com/site2/Page2.htm.
Thank you.
You need to proxypass to do something like that. You need to make sure that you have mod_proxy loaded in apache. Then use th P flag in the rule. You can try it like this.
RewriteCond %{REQUEST_URI} ^/site2 [NC]
RewriteRule ^(.*)/?$ http://89.89.89.89:8888/$1 [L,P]
To enable mod_proxy, you typically just need to uncomment these lines in apache config and reload/restart apache.
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_http_module modules/mod_proxy_http.so

500 internal error with RewriteEngine on .htaccess on localhost with wamp

I'm having a problem with a script. It doen't works with a htaccess file that is needed to work. Here's what the htaccess contains. I'm trying to install it on a wamp localhost. The code is:
#AddType x-mapp-php5 .php
#AddHandler x-mapp-php5 .php
RewriteEngine ON
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
#RewriteRule ^(.*)$ /index.php?q=$1 [L,QSA]
Options All -Indexes
If I remove this it works:
RewriteEngine ON
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
#RewriteRule ^(.*)$ /index.php?q=$1 [L,QSA]
But this way the script loads but every page show error 404. Is there a way to resolve this problem??
It looks like you don't have the rewrite modules loaded. Find your httpd.conf file and make sure this line (or something similar) is uncommented:
LoadModule rewrite_module modules/mod_rewrite.so
Check that you have the the apache rewrite module loaded.
go to wamp_manager -> apache -> modules and look for rewrite_module in the list.
If it does not have a TICK beside it click it. Apache will be bounced ( stop, start ). Try again.
The rewite engine will not work without the required module loaded.
I had the same problem. To un-comment the line, remove the # in front of the line
LoadModule rewrite_module modules/mod_rewrite.so
Worked for me in Wamp.
Directory of the httpd.conf file: C:\wamp\bin\apache\apache2.4.9\conf
This is one solution that solved the issue for me. Rewrite module was always enabled, used in IfModule rewrite_module, permissions were granted and .htaccess contents were fine, yet it still was 500 error trying to use rewrite module.
In httpd.conf by default:
This is a source of a 500 error if you try to use rewrite in .htaccess in some sub directory.
`
# Deny access to the entirety of your server's filesystem. You must
# explicitly permit access to web content directories in other
# <Directory> blocks below.
#
<Directory />
AllowOverride none
Require all denied
</Directory>
`
So one may want to use .htaccess with rewrite module in a specific directory. You would add a <directory> block for that directory. If one copies and pastes the directory block, you need to make sure the intent of the block you copy is correct for the directory you want to apply it to.
So for my intent this block, causes a 403 error, but does get rid of the 500 error.
<Directory "c:/Apache24/htdocs/store">
AllowOverride All
Options None
Require all granted
</Directory>
Changing to this solved the issue:
<Directory "c:/Apache24/htdocs/store">
AllowOverride All
Require all granted
</Directory>
I suppose this is why the issue is commonly seen, but rarely solved in these threads. If I simply copied a different block, typed my own block, or had any understanding of what I was doing, this wouldn't have been an issue.
I can't say this solves everybody's issue, but I hate when people solve-and-run w/o enlightening the rest of us. So for those that did my mistake, this is the answer.

Codeigniter doesn't work without index.php

I'm using MAMP with alias named works.
Codeigniter is at localhost/works/project/web
controllers doesn't work without index.php before them (localhost/works/project/web/index.php/auth/register)
$config['base_url'] = 'localhost/works/project/web/'; //with http://
$config['index_page'] = '';
$config['uri_protocol'] = 'AUTO';
(I tried all of them for uri_protocol)
I created and edit .htaccess file at /User/me/works/project/web/
I tired all the .htaccess files about this issue.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /Users/me/works/project/web
#Removes access to the system folder by users.
#Additionally this will allow you to create a System.php controller,
#previously this would not have been possible.
#‘system’ can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php/$1 [L]
#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn’t true it sends the
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#This last condition enables access to the images and css folders, and the robots.txt file
#Submitted by Michael Radlmaier (mradlmaier)
RewriteCond $1 !^(index\.php|images|robots\.txt|css)
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
# If we don’t have mod_rewrite installed, all 404’s
# can be sent to index.php, and everything works as normal.
# Submitted by: ElliotHaughin
ErrorDocument 404 /index.php
</IfModule>
creates error, which is:
[error] [client ::1] File does not exist: /Users/me/works/project/web/auth
it is same when I use RewriteBase /
mod_rewrite is active in phpInfo.
I couldn't find the solution.
Your RewriteBase is wrong. It should not be the relative file path for your server, it should be the relative URL path for your rewrite rules and such. More info here.
If you've ever used HTML's <base> tag, it's pretty much the same principle. If your main localhost URL is http://localhost, and your project is in the subfolder http://localhost/my-project, then you would use RewriteBase /my-project/.
If you didn't use RewriteBase, and your project is in a subfolder, you'd have to add the subfolder to every one of your URLs and rewrites, like this:
RewriteRule ^(.*)$ /my-project/index.php/$1 [L]
Food for thought:
Start your .htaccess files as simple as possible. The more you throw at it right away, the more that can go wrong, and the harder it is to debug. Especially if you're a noob to .htaccess files. Don't blindly copy and paste -- figure out what things actually do.
The system folder access part is redundant now -- the system folder has its own .htaccess to restrict requests, just like the application folder.
1.Make below changes in application/config.php file
$config['base_url'] = 'http://'.$_SERVER['SERVER_NAME'].'/Your Ci folder_name';
$config['index_page'] = '';
$config['uri_protocol'] = 'AUTO';
2.use this in .htaccess file:
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
and enable rewrite mode using below command
a2enmod rewrite
and Edit the file /etc/apache2/sites-enabled/000-default
change the AllowOverride None to AllowOverride All.
and finally Restart your server
You certainly don't have mod_rewrite on or you did not install it with MAMP
Open your httpd config file and change
# AllowOverride controls what directives may be placed in .htaccess files.
# It can be "All", "None", or any combination of the keywords:
# Options FileInfo AuthConfig Limit
#
AllowOverride None
To
AllowOverride All
uncomment this line:
#LoadModule rewrite_module libexec/apache2/mod_rewrite.so
Also make some changes to users here: /etc/apache2/users/username.conf
LoadModule php5_module libexec/apache2/libphp5.so
DocumentRoot "/Users/username/where/ever/you/like"
<Directory "/Users/username/where/ever/you/like">
Options Indexes MultiViews +FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
Get more info here http://adam.merrifield.ca/2010/08/09/apache-mod_rewrite-php-and-os-x-10-6-snow-leopard/
Restart MAMP
To find out if mod_rewrite is on, follow this answer in Stackoverflow:
How to check if mod_rewrite is enabled in php?
Just to be sure your .htaccess is not the issue, could you try this one. It's been tested:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /Users/me/works/project/web/
# If the start of the URL doesn't match one of these...
RewriteCond $1 !^(index\.php|robots\.txt|assets|cache|themes|uploads|css|images|js)
# Route through index.php
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
Access to certain folders are granted above and the rewritebase has a trailing slash.
Make sure you have any other nested .htaccess overriding this very one.
I fixed the problem by instead of using .htacces, I write them to httpd.conf.
I checked the AllowOverride is All but I'm not sure why .htaccess isn't working. Because of I lost too much time with this issue, I'm going to use this way.
Thanks for the answers.
Use AllowOverride All on your vhost:
<Directory "/path/to/your/site">
....
AllowOverride All
</Directory>
Open your httpd config file
LoadModule rewrite_module modules/mod_rewrite.so then AllowOverride All
<Directory "C:/xampp/htdocs"> //example for xampp
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride All
Order allow,deny
Allow from all
</Directory>
I have installed codeigniter in a url like the format: http://11.12.34.45/~project/
I tried different suggestions and finally this following url provided the solution:
https://ellislab.com/expressionengine/user-guide/urls/remove_index.php.html
You need to change two files of Apache,
httpd.conf
httpd-vhosts.conf
change this in httpd.conf
<Files ".ht*">
Require all denied
</Files>
to,
<Files ".ht*">
Require all granted
</Files>
and add below code in httpd-vhosts.conf,
<VirtualHost *:8000>
ServerName localhost
DocumentRoot c:/wamp64/www
<Directory "c:/wamp64/www/">
Options +Indexes +Includes +FollowSymLinks +MultiViews
AllowOverride All
Allow from all
</Directory>
</VirtualHost>

.htaccess help - not working

I want to rewrite all .php into .html,,, so i created a .htaccess file and added
AddHandler application/x-httpd-php .php .html .htm
but when it seems not working...
here i uploaded all files - http://www.fellowindian.com/ca/index.php & http://www.fellowindian.com/ca/page1.php
1: Do you have mod_mime installed on Apache.
2: Are you sure that .htaccess is executing.
Simple test would be to see if it can rewrite your urls to add / remove www from it.
Example:
Options +FollowSymLinks RewriteEngine
On RewriteBase / RewriteCond
%{HTTP_HOST} !^www\.mycee\.com$ [NC]
RewriteRule ^(.*)$
http://www.mycee.com/$1 [R=301,L]
3: Which user is the owner of the .htaccess file and what is its attributes?
4: Check that the AllowOverride directive is set in your apache config and not set to None.
Test by putting invalid directives in the .htaccess file and reloading the page.
If the apache error log doesn't show any errors, it's not executing.
5: If you're on shared hosting, check with your host if they have AllowOverride enabled or not.
Personally, I think the best place to put the AddType directive would be in apache's httpd.conf as .htaccess puts a performance hit on your server, but in the case of shared hosting, .htaccess is usually the only option available.
If you're trying to redirect something.php to something.html, you could do
RewriteEngine On
RewriteCond %{REQUEST_URI} .php$
RewriteRule (.*?).php $1.html

Resources