Change site URL in Apache with mod_rewrite - apache

How can i change my site URL using mod_rewrite my problem is when i open my site's admin section i have to write www.example.com/index.php/admin
what i want is i directly open my site like www.example.com/admin please help
thanks

Here's a .htaccess file copied from the CodeIgniter PHP framework:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
This redirects everything that isn't index.php, robots.txt or anything in the images folder to index.php/$1, where $1 is the URL the user entered.
You need these rules as:
Redirecting index.php would cause an infinte loop - http://localhost/index.php/index.php/index.php/...
robots.txt is an important file that search engines use. It is just plain text; you don't want it handled by your code.
You obviously want to keep having access to your images. Adjust this path as necessary for where your static assets are located.

Try this rule:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule !^index\.php/ index.php%{REQUEST_URI} [L]
The condition is to exclude requests of existing files to be rewritten.

Related

Change root directory in .htaccess

I have a problem with changing the root directory in .htaccess.
My folder structure looks like this.
What I want to achieve is, when I visit this page:
/comparty/about/
The page I will see is this page:
/comparty/pages/about/
I have already tried to search on Google, but the code I found did not work, though I tried to change it:
RewriteEngine On
RewriteBase /comparty/
RewriteRule ^(.*)$ pages/$1 [L]
I don't want it to redirect, I want to keep the same URL. Also I've had a big problem with Apache caching the .htaccess file, so I haven't been able to test many things.
Thanks in advance.
EDIT
I found a way to rewrite the URL from /comparty/pages/about/ to /comparty/about/ - this is the code:
RewriteEngine On
RewriteBase /comparty/
RewriteRule ^about/(.*)$ pages/$1 [L]
This only works on the about page, though. What would I have to do, to make it dynamic and work with every page?
You need to use a dynmic pattern :
RewriteEngine On
RewriteBase /comparty/
#if the request is not for an existent dir
RewriteCond %{REQUEST_FILENAME} !-d
#and the request is not for an existent file
RewriteCond %{REQUEST_FILENAME} !-f
#rewrite the request to "/pages/request"
RewriteRule ^(.*)$ pages/$1 [L]
RewriteConditions above are important to avoid rewriting your existent files and directories to the /pages subfolder. Without those conditionrt the Rule will rewrite all requests including the destination path /pages and this may result in rewrite loop error.

Apache Mod_Rewrite Seems to be Causing Javascript Reloads

I'm setting up URL rewrite rules for an application I'm developing so that I can use nice clean URLs. I want the URLs to look like http://app.com/page/agency/ and to be equivalent to http://app.com/index.php?p=page&agency=agency. The agency selector is optional, so I want the URLs to redirect, even if the agency is not present. I have created the following mod_rewrite rules for this purpose:
RewriteRule ^/?([a-z]+)/$ /index.php?p=$1 [PT]
RewriteRule ^/?([a-z]+)/([a-z]+)/$ /index.php?p=$1&agency=$2 [PT]
This is working fine for redirecting the pages. However, it seems to me that my javascript files are being re-loaded with each page, as if the browser thinks that it's in a different directory and needs to re-load the JS files. The JS files are linked using a hard-coded URL, such as http://app.com/scripts/dostuff.js.
Is it possible that the browser is reloading the javascript files each time? If so, have I done something wrong?
Try this code:
RewriteEngine On
# skip rewrite rules below it is a valid file or a valid directory
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
# write single path
RewriteRule ^([a-z]+)/?$ /index.php?p=$1 [L,QSA]
# write 2 paths
RewriteRule ^([a-z]+)/([a-z]+)/?$ /index.php?p=$1&agency=$2 [L,QSA]

Using Apache mod_rewrite to send all requests to a file

How can I use mod_rewrite to send certain types of incoming urls to a particular file on the server?
Ideally any html requests would load a file (eg /var/www/sites/urlprocessor.php) which would then determine what is being requested and the display the appropriate content based on the url.
Example: Say I wanted a url to list some 'cars'. I would want the following URLs to load the urlprocessor.php file instead of loading the actual page and instead of doing a redirect.
http://www.mydomain.com/cars.html --> /var/www/sites/urlprocessor.php
http://www.mydomain.com/cars.htm --> /var/www/sites/urlprocessor.php
http://www.mydomain.com/cars --> /var/www/sites/urlprocessor.php
On the flip-side, non-html requests (jpgs, pdfs, etc) would continue on normally without loading the urlprocessor.php file.
I am currently using the last line of code in the following to accomplish this. It 301 redirects all html page requests to 'index.html' which then loads the urlprocessor.php file. I prefer to just load the file without the 301 redirect.
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^mydomain\.com
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [R=301,L]
RewriteRule !^.*(\.gif|\.GIF|\.png|\.mp3|\.m3u|\.js|\.jpg|\.JPG|\.css|\.swf|\.flv|\.ico|\.wav|\.doc|\.DOC|\.pdf|\.PDF|\.xls|\.XLS|\.txt|\.TXT)$ index.html [L]
Is there a way to go directly to a file without doing the redirect?
Thanks!!
Tom
Given that /var/www/ is the root folder then this would work:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . /sites/urlprocessor.php [L]
Basically this will redirect any non-existent folder and file to the urlprocessor.php
For more information on how to handle the redirect with your PHP you can refer yourself to this answer.

How to limit access to .zip files to all but a specific folder with .htaccess

I have a download site where I blocked access to .zip files in order to force users to go through my download process (simply means u have to register before you download).
Now I am also making a java arcade and I want the applet to be able to download zip files from a specific directory on it and play them.
Right now I am using something like in my site's root .htaccess:
RewriteRule .*\.(zip)$ http://www.google.com [R,NC]
The question is how do I tell it not to include the folder "arcade" in it.
What do I modify in the above code or what do I put in the arcade/.htaccess file to make it possible to link to zips..
Thanks!
- Ben
You should use the RewriteCond directive. You need something like:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/arcade/
RewriteRule \.zip$ - [F]
I have also changed the way you block access to .zip files. It's better to return a 403 error code.
Try
#exclude the arcade folder
RewriteCond %{REQUEST_URI} !^/arcade [NC]
RewriteRule .*\.(zip)$ http://www.google.com [R,NC]
Instead of the redirect you could also give a 404 as below. Also simplified the match for the RewriteRule
RewriteCond %{REQUEST_URI} !^/arcade [NC]
RewriteRule \.zip$ - [R=404,NC]

Ignore Rewrite Rule for images folder

I have a .htaccess file on a website I'm working on which rewrites urls from mydomain.com/sub/folder/ to mydomain.com?index.php?controller=sub&view=folder
Unfortunately the way I've written it means I can't access images, stylesheets and other linked files anymore. Could anyone tell me how best to exclude specific directories / URL requests from the rewrite rule?
Apologies if this is a bit of a newbie question, I'm still wrapping my head around this mod rewrite stuff!
The .htaccess file looks like this:
RewriteEngine on
RewriteRule ^([a-zA-Z0-9-]+)/?$ index.php?Controller=$1
RewriteRule ^([a-zA-Z0-9-]+)/([a-zA-Z0-9-_]+)/? index.php?Controller=$1&View=$2
If your images are in mydomain.com/images and you are linking to them using relative links on the page mydomain.com/sub/folder/ the browser is going to try to attempt to access the image via mydomain.com/sub/folder/images/i.gif. But if you change your links to absolute links, the browser will correctly attempt to load mydomain.com/images/i.gif. However, the RewriteRule will change it to: mydomain.com/index/php?Controller=images&View=i.gif. To avoid this you need to add a few RewriteConds:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9-_]+)\/?$ index.php?Controller=$1
RewriteRule ^([a-zA-Z0-9-_]+)/([a-zA-Z0-9-_]+)\/? index.php?Controller=$1&View=$2
So that when attempting at access an existing file/directory, don't rewrite to index.php.