apache mod_rewrite redirection problem - apache

ok i am having a problem with redirection on apache, i have a domain configured on my hosting account but the domain needs to be redirected to a folder. eg: / is root of server where the mysite.com answers /mysite is where the files are so i got this htaccess code to do the job:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?mysite.com$
RewriteCond %{REQUEST_URI} !^/mysite/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /mysite/$1
RewriteCond %{HTTP_HOST} ^(www.)?mysite.com$
RewriteRule ^(/)?$ mysite/index.php [L]
plus i made an index.php to redirect to mysite folder.
everything seems to be working good the only problem is i added a forum on /mysite/forums/
and for some reason instead of getting mysite.com/forums/ in the browser im getting mysite.com/mysite/forums/
could anyone help me solve this problem?
Thanks in advance!

I would try writing the full address, so something like:
RewriteRule ^(.*)$ http://www.site.com/$1
and
RewriteRule ^(/)?$ http://www.site.com/index.php [L]

ok i got what was wrong and i thank everyone that had a look at it.
Solution:
Change last line
RewriteRule ^(/)?$ mysite/index.php [L]
to
RewriteRule ^(/)?$ mysite/$1 [L]
so it answer to anything coming from that folder not just the index.php :)

Related

Apache RewriteRule not working – page not found

I have no idea why it doesn't work, /spelling/30000 gives the Not Found page. Please help.
RewriteEngine On
RewriteBase /
RewriteRule ^/([0-9]+)$ /?mod=spelling&word=$1 [PT]
RewriteRule ^$ /?mod=spelling [PT]
With your shown samples please try following .htaccess rules file. Considering that index.php file is the one which is taking traffic in backend for internal rewrite rules. Please make sure to keep your .htaccess file along side with index.php file.
Also make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteBase /
RewriteRule ^/?$ index.php?mod=spelling [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/?$ index.php?mod=$1&word=$2 [QSA,L]

.htaccess Messy Rewrite Rules

I could do with some assistance trying to clean up an .htaccess file that has been running on one of our servers at work.
It is currently set up so that if someone types example.com it will redirect to www.example.com. The problem is that we want to utilise subdomains but when we try to add a subdomain, like beta.example.com it will redirect to www.beta.example.com
Here is the .htaccess file
DirectoryIndex index.html index.php
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !www.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
I have tried multiple variations from questions here on SO and via Google and htaccess generators and none of them seem to help as they usually put the site in a redirect loop.
Any help on getting the configuration correct would be appreciated.
-- Chris
To automatically add a www to your domain name when there isn't a subdomain, add this to the htaccess file in your document root::RewriteEngine On
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=301]

Subdomain pointed to controller - htaccess (CakePHP?)

I was looking for quite a while but I couldn't come up with right solution to my problem which seemed to be quite easy at first.
So I got a website based on CakePHP and I want to point some sub domains to matching controllers. For example:
hello.domain.com pointed to StaticPages/hello/ (no redirection; please note that it's not a physical directory)
hello.domain.com/(.*) redirected to domain.com/(.*) (with redirection)
Here's CakePHP's standard .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>
Could you help me guys with that? I really got stuck on that.
Thanks in advice,
Mike
-- Edit:
I finally managed with some help to get following rules:
RewriteCond %{HTTP_HOST} ^sub\.domain\.com$
RewriteCond %{REQUEST_URI} ^/$ [OR]
RewriteCond %{REQUEST_URI} ^/StaticPages/hello/$ [OR]
RewriteRule ^(.*)$ /StaticPages/hello/ [L]
RewriteCond %{HTTP_HOST} ^sub\.domain\.com$
RewriteCond %{REQUEST_URI} ^/(.*)$
RewriteRule ^(.*)$ http://domain.com/$1 [L]
However I got 500 error, as I suppose because I've created an infinite loop when requesting sub.domain.com/StaticPages/hello/
Do you perhaps have any ideas how to fix it?

Rewrite URL with subdirectory and file

i am trying to use .htaccess to rewrite the URLs for my site, here is what i would like
currently, the urls look like this
http://www.mysite.biz/store/cart.php?m=new_arrivals
All of the sites are loaded using php GET and the only thing that ever changes is the query string after cart.php so i would like to rewrite to....
http://www.mysite.biz/cart.php?m=new_arrivals
I have been searching all over the internet and have tried out various techniques to do this and none of them have worked. i've tried this...
RewriteRule ^store/(.*)$ http://www.mysite.biz/$1 [R=301,L]
this...
RewriteCond %{REQUEST_URI} ^/store($|/)
RewriteRule ^.*$ /store/cart.php [L]
and this...
RewriteCond $1 !^store
RewriteRule ^(.*) /store/$1 [L]
ive been developing web for a while now but this is my first time really doing anything with .htaccess and i am totally stumped. any help will be greatly appreciated
Try this rule in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+store/(cart\.php[^\s]+) [NC]
RewriteRule ^ /%1 [R=302,L]
RewriteRule ^(cart\.php)$ /store/$1 [L,NC]

Protected directory issue on web server

When I setup a password protected directory in cpanel for my website, if I use www.example.com/protected it correctly prompts for a username and password but if I use example.com/protected it lets me access the proctected area.
The main site .htaccess is:
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} .*\.(jpeg|jpg|gif|png)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . /public/404.php [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Can anyone point me in the right direction to fix this please?
Many Thanks
Jim
You need to tell this to apache by prepending the following rule before your rules to redirect a url without www to a one with www
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301]
There would be no difference between www and non-www. Your issue is probably browser cache, or you logged into the non-www version and forgot.