mod_rewrite in .htaccess issues - mod-rewrite

I have sniffed through quite a few postings about mod_rewrite this turned out more complex than I was expecting.
What I am trying to do is the following.
I have 10 cities with events, and each city should have their own "directory" such as:
site.com/toronto/
site.com/chicago/
The events could have up to 3 sub levels such as
site.com/toronto/musiccamp/sub18
site.com/chicago/bootcamp
And I need these URLs mapped to the page
site.com/camps/index.php?city=toronto&type=musiccamp&ages=sub18
site.com/camps/index.php?city=chicago&type=bootcamp
Other directories, such as
site.com/about - site.com/help etc
should NOT be rewritten.
This has to be within an .htaccess file.
So far I got this going (in godaddy servers)
#Fix Rewrite
Options -Multiviews
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /test/index.php?option1=$1 [L]
However I am unable to pass the options to index.php... they are always null...

In the end, this is what worked best in my case
#Fix Rewrite
Options -Multiviews
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /test/index.php?option1=%{REQUEST_URI}
That way any path that does not exist will get rewriten to the /test/index.php file and can be handled from there.
Passing the REQUEST_URI allows me to build N level deep and have the index.php file parse it out.
Hope this helps someone.

Related

.htaccess to provide 'flattened' search engine friendly CMS URLs

I am using CMSMADESIMPLE for a website. It can generate search engine friendly URLs using a .htaccess file and mod_rewrite.
BUT it is possible to insert anything between the base url and the requested page. For example all these URLs will work:
www.example.com/aboutus
www.example.com/home/aboutus
www.example.com/any/rubbish/i/enter/aboutus
I have seen other sites using Wordpress where it is possible to enter the same in the address window, but it will redirect back to the lowest level i.e. www.example.com/aboutus. I would like to achieve the same thing but cannot master .htaccess to do it.
The relevant bits of the .htaccess file are:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?page=$1 [QSA]
</IfModule>
I am assuming that this is something that can be achieved just using mod_rewrite and doesn't require changes to the PHP code within the CMS.
If that's not possible I would like to setup a permanent redirect from some URLs within the CMS which have been indexed by Google.
i.e From www.example.com/home/aboutus to www.example.com/aboutus
I tried adding a RewriteRule to .htaccess without success:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteRule home/aboutus aboutus [R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?page=$1 [QSA]
</IfModule>
Any help appreciated.
Look at the sample htaccess.txt file in the doc folder. There is a section about parent child.
# Rewrites urls in the form of /parent/child/
# but only rewrites if the requested URL is not a file or directory
#
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?page=$1 [QSA]

Removing index.php and handling subdomains of two Codeigniter sites, when one within the other

I have two Codeigniter sites, one sitting within the subdirectory of the other. I need some help modifying my htaccess file to remove index.php from both.
The first site, http://subdomain.domain.com is stored in /home/sites/subdomain/www/html/
The second, http://test.subdomain.domain.com lives in /home/sites/subdomain/www/html/app_staging/
This is my .htacess file from /home/sites/subdomain/www/html/:
RewriteEngine On
# Rewrite the main domain
RewriteCond %{HTTP_HOST} !test.subdomain.domain.com
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]
# Rewrite the sub domain
RewriteCond %{HTTP_HOST} test.subdomain.domain.com
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/(.*)$ /app_staging/index.php?/$1 [L]
This is removing index.php from the from the first site, everything is ok there. On the second site, I can load the default controller form http://test.subdomain.domain.com, but subsequent pages return a 404 from the server. What are my next steps?
You should put each of two .htaccess files inside their own root CI folder.
Personally, I prefer restricting requested files and/or folders in order to prevent them from being treated by RewriteRule.
So, for the first file which is located at /home/sites/subdomain/www/html/:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine on
RewriteCond $1 !^(index\.php|robots\.txt|favicon\.ico|public)
RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>
And for the other one is located at /home/sites/subdomain/www/html/app_staging/:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine on
RewriteCond $1 !^(index\.php|robots\.txt|favicon\.ico|public)
RewriteRule ^(.*)$ /app_staging/index.php/$1 [L]
</IfModule>
Note: You must add css and other folders (like public) to RewriteCond if you don’t want them to be treated by RewriteRule.
As a side-note: You might want to prevent directory listing by using the following inside each .htaccess file:
<IfModule mod_autoindex.c>
Options -Indexes
</IfModule>

.htaccess syntax code needing revision

1) i am trying to 301 all non www's to www's (including files)
e.g. /subdirectory/ + /subdirectory/1.jpg (all possibilities with the www.)
2) i am trying to exclude ONLY but ALL .html and .php files from showing and would like only the non trailing version to be indexed, the rest to be 301's to my domain
e.g. /example.html or .php >> /example
e.g. /example.html or .php >> to not work + to not be indexed
e.g. /example to ONLY work and to ONLY be indexed (to avoid duplicate content)
3) i am trying to 301 all dead links and 404's to my domain
e.g. /deadlink or /deadlink.pdf >> 301'd to my domain, example.com
Here is the code i currently have, however i am not sure if it's 100% proper.
Can someone please reply with a validated syntax for these 3 tasks? Thanks.
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . / [L,R=301]
OK, you've got three parts you're trying for here, and I'll address them all separately.
Before we begin, you only need to have one
RewriteEngine on
It only needs to be turned on once.
You should also set:
Options -MultiViews
as MultiViews can otherwise cause some weird issues here.
Redirect to WWW
Your current rules should work, but you can simplify them a little using the != prefix on RewriteCond:
RewriteCond %{HTTP_HOST} !=www.example.com
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
Removing .php and .html suffixes
You need two separate sets of rules here: one to remove suffixes from URLs, and another to readd them internally.
Let's do the second half first, since that's easier:
RewriteCond %{SCRIPT_FILENAME}.html -f
RewriteRule ^(.*)$ $1.html [L]
RewriteCond %{SCRIPT_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [L]
With this in place, though, removing suffixes is a little tricky without conflicting with the rule that goes ahead and tries to put them back on. The best approach I was able to come up with was:
RewriteCond %{THE_REQUEST} \.(html|php)
RewriteRule ^(.*)\.(html|php)$ $1 [R=301,L,NC]
But I'm open to suggestions.
301ing all dead links
Don't do that. If a link doesn't exist, it should return a page with a 404 status. If you want to redirect visitors to your home page from dead links, put a link in your 404 page.

apache mod rewrite on a subdirectory

quick question about mod rewriting on a sub-directory when i already have a rewrite rule on the webroot to remove 'index.php'
my .htaccess looks like this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
i have an API directory (/webroot/api/index.php) where i want to have a RESTful API (Restler) to serve requests into the application- so my question is..
to make RESTful requests i need to use the following URL format:
/webroot/api/index.php/user/get/ (to get all the users)
what i would like to achieve is something in the form:
/webroot/api/user/get/ (return a list of users)
which still retaining the base rewrite rules for the main website
/webroot/members/ (display a list of users in the main applications)
many thanks,
Justen
Your base rewrites look odd.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
This says that if we match index.php we are done (and no rewrite takes place). Is that what you want? If the request is exactly for /index.php with no trailing material, then it is left alone.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
To remove index.php you want something like:
RewriteRule ^index.php/(.*) $1 [L]
E.g. index.php/foo goes to foo (relative rewrite: then RewriteBase gets tacked on it turning into the /foo URL to feed back into the request pipeline.)

Request exceeded the limit of 10 internal redirects due to probable configuration error.?

i have a simple rewrite
RewriteRule ^.*$ addnew/$0
however i get the
Request exceeded the limit of 10 internal redirects due to probable configuration error.
I am trying to rewrite
www.mysite.com/anycharacter
into
www.mysite.com/addnew/anycharacter
Try the folllowing solution, it works for me.I hope this will work for you too.
BEFORE DOING ANYTHING - BACKUP YOUR DB!
Now, go into wordpress > settings > permalinks
Set it to one of the pretty permalink settings like //year/month/'
Open a text editor on your PC/Mac and open the .htaccess file you downloaded from your
webserver
Find this piece of code
# BEGIN Wordpress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Replace it with this piece of code, courtesy of Scott Yang:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ /index.php/$1 [L,QSA]
</IfModule>
Upload this code into the main domain root directory on your server and you're done.
Everything should work
As RC already said, .* will also match addnew/. And since the L flag causes a reinjection of the rewritten rule, you will get an infinite recursion.
So adjust the rule so it doesn’t match your addnew/:
RewriteRule !^addnew/ addnew%{REQUEST_URI}
.*matches addnew/. Try with:
RewriteRule ^[^/]*$ addnew/$0

Resources