.htaccess subdomain traffic to folder and sub folders - .htaccess

Ok long story short:
http://subdomain.domain.com/test1/test2
needs to display the information here:
http://domain.com/agents/subdomain/test1/test2
and obviously if you with this
http://subdomain.domain.com/
needs to display
http://domain.com/agents/subdomain/
Here's what I have so far:
RewriteEngine On
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]
RewriteCond %{HTTP_HOST} !^(www\.)?domain\.com [NC]
RewriteCond %{HTTP_HOST} ([^\.]+)\.domain\.com [NC]
RewriteCond %{REQUEST_URI} !^/agents
RewriteRule ^(.*)$ /agents/%1/$1/ [L]
I need the first rule to get rid of the index.php from the URL.
I can get the first piece done http://subdomain.domain.com/ but not the sub folders.
the above syntax is throwing an internal server error but if I change the last line to:
RewriteRule ^$ /agents/%1/$1/ [L]
it works except I can't get to sub directories.

Ok finally figured this out... here's the solution:
RewriteEngine On
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} ^(www\.)? domain\.com [NC]
RewriteRule ^(.*)$ /index.php?/$1 [L]
RewriteCond %{HTTP_HOST} !^(www\.)? domain\.com [NC]
RewriteCond %{HTTP_HOST} ([^\.]+)\. domain\.com [NC]
RewriteCond %{REQUEST_URI} !^/index.php
RewriteRule ^/?(.*) /index.php?/agents/%1/$1 [L]

Related

htaccess redirects affect administrator page on Joomla

I have a task - all somestuff.html/ pages with trailing slash must redirect to somestuff.html and all other pages without a trailing slash must redirect to add a trailing slash (e.g. /mypage must redirect to /mypage/). I have accomplished the task but now /administrator/ page returns 404 page. I tried to exclude the /administrator/ page but its not working:
DirectoryIndex index.php
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#this part breaks the admin panel entry
RewriteRule ^([^.]+).html/ http://somedomain.com/$1\.html [R=301,L]
RewriteCond %{REQUEST_URI} !\.(php|html?|jpg|gif)$
#trying to exclude administrator page
RewriteCond %{REQUEST_URI} !^/administrator/
RewriteRule ^(.*)([^/])$ http://%{HTTP_HOST}/$1$2/ [L,R=301]
#end of part that breaks the admin panel entry
RewriteCond %{REQUEST_URI} !^/index.php
RewriteCond %{REQUEST_URI} (/|\.php|\.html|\.htm|\.feed|\.pdf|\.raw|/[^.]*)$ [NC]
RewriteRule (.*) index.php
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L]
Can anyone tell me what I am doing wrong or maybe offer some different solution to acocmplish the redirecting. Thanks.
Found a solution:
DirectoryIndex index.php
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^.]+).html/ http://logosstudy.ru/$1\.html [R=301,L]
RewriteCond %{REQUEST_URI} !^/administrator/
RewriteCond %{REQUEST_URI} !\.(php|html?|jpg|gif)$
RewriteRule ^(.*)([^/])$ http://%{HTTP_HOST}/$1$2/ [L,R=301]
RewriteCond %{REQUEST_URI} !^/administrator/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/index.php
RewriteCond %{REQUEST_URI} (/|\.php|\.html|\.htm|\.feed|\.pdf|\.raw|/[^.]*)$ [NC]
RewriteRule (.*) index.php
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L]

RewriteCond blank domain to the domain with the correct path

How do I set redirects with following conditions:
Url http://domainname.be becomes http://www.domainname.be/nl/
Url http://www.domainname.be becomes http://www.domainname.be/nl/
Url http://domainname.be/nl/custompage/ becomes http://www.domainname.be/nl/custompage/
?
Rewrite conditions
RewriteCond %{HTTP_HOST} ^domainname.be [NC]
RedirectRule ^(.*)$ http://www.domainname.be$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^domainname.be [NC]
RewriteCond %{REQUEST_URI} ^/$
RedirectRule ^(.*)$ http://www.domainname.be/nl$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^www.domainname.be [NC]
RewriteCond %{REQUEST_URI} ^/$
RedirectRule ^(.*)$ http://www.domainname.be/nl$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/nl/custompage /Custompage.aspx?lang=nl [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/nl/custompage/ /Custompage.aspx?lang=nl [L,NC]
But is doesn't work. Also, do you need to clear the browsercache in order to have the latest conditions?
Remove all of your existing rules and use this:
RewriteCond %{HTTP_HOST} ^(www\.)?domainname\.be$ [NC]
RedirectRule ^((?!nl/).*)$ http://www.domainname.be/nl/$1 [R=301,L]
And yes test it out in a different browser to avoid 301 caching issues.

301 redirection through .htaccess file not working

I'm using CodeIgniter.
in httacess file, I wrote this code to remove index.php from url:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
Now I want to redirect site root to subdomain, with this code:
rewritecond %{http_host} ^domain.com [nc]
rewriterule ^(.*)$ sub.domain.com/$1 [r=301,nc]
domain.com redirected to sub.domain.com successfully. but domain.com/dir to sub.domain.com/dir not work and still opening domain.com/dir.
When I removed first code, redirection worked perfect, but I need to remove index.php too.
Adding $to the end of rewritecond %{http_host} ^domain.com [nc] should fix it for you.
rewritecond %{http_host} ^domain.com [nc]
rewriterule ^(.*)$ sub.domain.com/$1 [r=301,nc]
Would become
rewritecond %{http_host} ^domain.com$ [nc]
rewriterule ^(.*)$ sub.domain.com/$1 [r=301,nc]
Have your rules like this:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^(.*)$ http://sub.domain.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]

How can I write my redirect so it does not show the directory?

If I'm visiting my website for the first time then it always displays the directory that I'm pointing my site to. For example, if I visit example.com, it redirects to example.com/site.2014/. If I remove the directory from the URL, and revisit, it doesn't show up anymore. Can anyone explain why this happens? I have the following information in my htaccess file:
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1%{REQUEST_URI} [R=301,QSA,NC,L]
RewriteCond %{HTTP_HOST} ^(www.)?example.com$ [NC]
RewriteCond %{REQUEST_URI} !^/site.2014/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /site.2014/$1
RewriteCond %{HTTP_HOST} ^(www.)?example.com$ [NC]
RewriteRule ^(/)?$ site.2014/ [L]
You can use this code:
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1%{REQUEST_URI} [R=301,L,NE]
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/site.2014/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /site.2014/$1 [L]
Make sure to test in a new browser.

Htaccess redirect question

I have a site I've written in php.
The addresses are as follows:
http://www.site.com/page.php
I'd like any request to:
www.site.com/page.php
or
www.site.com/page
To go to:
www.site.com/page/
(And force a www.)
Can anyone help me?
For domain :
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]
For your first directory :
RewriteRule ^/([^/]*)(\.php)? /$1/
RewriteRule ^page.php$ http://www.site.com/page/ [QSA,L]
RewriteRule ^page$ http://www.site.com/page/ [QSA,L]
Maybe this will work.
This should do what you wanted:
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]+\s/([^\s]*?)\.php [OR]
RewriteCond %{THE_REQUEST} ^[A-Z]+\s/([^\s]*?[^/\s])\s [OR]
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST}/%1 ^(www\.)?(.*?)/?$
RewriteRule ^ http://www.%2/ [R=301,L]
# Assuming you want to write the request back to page.php too...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)/$ $1.php
Tim's answer is the closest, but it also does adds the trailing slash to images and css...
So taking Tim's answer, and slightly editing it gives us:
RewriteEngine on
RewriteRule \.(css|jpe?g|gif|png)$ - [L]
RewriteCond %{THE_REQUEST} ^[A-Z]+\s/([^\s]*?)\.php [OR]
RewriteCond %{THE_REQUEST} ^[A-Z]+\s/([^\s]*?[^/\s])\s [OR]
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST}/%1 ^(www\.)?(.*?)/?$
RewriteRule ^ http://www.%2/ [R=301,L]
# Assuming you want to write the request back to page.php too...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)/$ $1.php
Which should work!

Resources