I have added a force redirect from non-www to www on my codeigniter based web app.
After doing that it is working fine, but its showing an extra index.php in URL which I need to remove.
Here is my URL : http://www.testprofile.com (WORKING FINE)
Here is problem caused :
https://testprofile.com/result/184/1103511096450940jawadamin (non www site, it will automatically add www.)
opening the above URL redirects to www but also adds an additional index.php in URL which should be removed.
HERE IS THE CODE OF MY .HTACCESS
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
RewriteCond %{HTTP_HOST} ^testprofile.com [NC]
RewriteRule ^(.*)$ https://www.testprofile.com/$1 [L,R=301]
</IfModule>
Please help me remove this annoying index.php
Thanks,
You missed to escape dot in right hand side of rewrite condition line. It should be like
RewriteCond %{HTTP_HOST} ^testprofile\.com [NC]
or in your whole example
<IfModule mod_rewrite.c>
RewriteEngine On
#If in root of web server you can use next line or set it appropriate according to location on server
#RewriteBase /
RewriteCond %{HTTP_HOST} ^testprofile\.com [NC]
RewriteRule ^(.*)$ https://www.testprofile.com/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
If dot is not preserved by back slash character apache read it as wild card character. You can find some useful tips on this page.
Also, you can check useful .htaccess snippets here.
I'm trying to remove the ".php" extension from the URLs of a site using this code (which I admit I copy/pasted from other questions here) in the .htaccess file:
RewriteEngine on
Options +FollowSymLinks
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
Other lines of the .htaccess file do work, for instance, I have an error redirect and:
RewriteCond %{HTTP_HOST} ^www\.domain\.com$ [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [R=301,L]
So, I know the .htaccess file is in service in general.
I don't know what could go wrong in this area, so I'm not sure where to begin troubleshooting. Does anyone have pointers?
Thanks in advance.
Given that your domain account is /home/youraccount/public_html, your .htaccess would be inside the public_html folder with the following content:
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteBase /
# First we redirect the www to non-www
RewriteCond %{HTTP_HOST} ^www\.domain\.com$ [NC]
RewriteRule ^/?(.*)$ http://domain.com/$1 [R=301,L]
# now we redirect phpless URLs internally to the php
# if folder does not exist
RewriteCond %{REQUEST_FILENAME} !-d
# but the file exists and ends with .php
RewriteCond %{REQUEST_FILENAME}\.php -f
# redirect to name.php
RewriteRule ^([^/]+)/?$ $1.php [L]
NOTE: If you have more rules this may conflict so I would have to look at the rest of your rule but basically the above should work as expected.
You will be able to access both:
domain.com/index
and
domain.com/index/
And it would redirect to your file index.php.
I have a codeigniter installation at example.com/ci.
I have a subdomain foo.example.com. The document root for the foo subdomain is set to be home/public_html/ci.
I'm using the following rule in .htaccess to send requests for foo.example.com to example.com/ci/city/foo.
RewriteCond %{HTTP_HOST} !^(www)\. [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.example\.com [NC]
RewriteRule (.*) http://example.com/ci/city/%1/$1 [L]
It all works like I want it to except that the address bar url changes from foo.example.com to example.com/ci/city/foo. I would like it to remain foo.example.com. There is no R=301 in the RewriteRule (used to be but I removed it). The .htaccess file is in the ci/ folder and the rule is above all the codeigniter stuff.
The redirect works perfectly and the url remains foo.example.com with (Jon Lin's answer)
RewriteCond %{HTTP_HOST} !^(www)\. [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.example\.com [NC]
RewriteCond %{REQUEST_URI} !/city/
RewriteRule (.*) /city/%1/$1 [L]
but the codeigniter default controller is called instead of the foo method in the city controller.
Any help is appreciated.
When your rewrite rule's target has an http://example.com in it, a 302 redirect is implicit regardless of whether an R flag is used or not. You need to provide the URI path based on the subdomain's document root, so I'm assuming you want something like:
RewriteCond %{HTTP_HOST} !^(www)\. [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.example\.com [NC]
RewriteCond %{REQUEST_URI} !/city/
RewriteRule (.*) /city/%1/$1 [L]
If the subdomain's document root is in the /ci/ directory.
The other option is to use the P flag to reverse proxy the request:
RewriteCond %{HTTP_HOST} !^(www)\. [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.example\.com [NC]
RewriteRule (.*) http://example.com/ci/city/%1/$1 [L,P]
Your mileage may vary with this (might need to finesse it to fit your server and conditions), but doing some testing on my Mac, here's what I had mild success with:
Directory Structure
public_html/
ci/
application/
system/
.htaccess
index.php
I'm assuming that you have other stuff in your root public_html directory. So I'm letting the .htaccess focus on the CodeIgniter-related stuff by leaving it in the ci dir.
.htaccess
DirectoryIndex index.php
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*)\.ciwildsub\.dev [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php/city/%1/$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
It's fairly self explanatory, but the first block is your subdomain check. I didn't bother excluding www but you may want to (as I said, your mileage may vary). The second block is a standard CodeIgniter index.php removal.
These rules will only apply to sub.example.com or example.com/ci/ URLs, since as I said, I assume your root has stuff that shouldn't be disturbed by rewrites.
CodeIgniter Config
$config['uri_protocol'] = 'PATH_INFO';
Because of the way Apache handles a URL like example.com/index.php/controller/method, it bypasses the index.php and handles it like any other directory segment. Also, mod_rewrite doesn't necessarily stop at the [L] tag -- it stops processing the .htaccess at that point, passes through the RewriteRule, and then runs that URL through the .htaccess. Setting PATH_INFO helps make sure CodeIgniter pulls the current URI correctly, and our .htaccess doesn't get stuck in a validation loop.
I will note, though, that I'm not entirely happy with what I see in my RewriteLog output -- there has to be a way to optimize this further, I'm just not sure of it yet (I'm done tinkering with this for today!). Sorry if any of the explanation here is a little out of whack - I'm not a server admin or mod_rewrite expert, I've just had fun tinkering with this. If I manage to find a better solution, I'll be sure to update this.
Looks like the END flag would be perfect for situations like this (to prevent [L] loops), but it's only available in Apache 2.3.9+. The search continues.
I got it to work correctly using the following rewrite rule
RewriteCond %{HTTP_HOST} !^(www)\. [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.example\.com [NC]
RewriteCond %{REQUEST_URI} !/city/
RewriteRule (.*) /city/%1/$1 [L]
and by setting
$config['uri_protocol'] = 'ORIG_PATH_INFO';
in the codeigniter config file. Thanks for all the help.
This worked for me
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /file_path/to/subdomain
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 /index.php
</IfModule>
I have a drupal website which has been split up to two separate websites, and now I need to setup some rewrite rules in order to drive traffic to the new site.
Original website looks like this:
http://www.website.com (frontpage)
http://www.website.com/web1/subpage1 (subpage)
http://www.website.com/web1/subpage2 (subpage)
http://www.website.com/subpage3 (subpage)
http://www.website.com/subpage4 (subpage)
All references to subpages that are not in the web1-category have been removed from the website, but the pages are still published and they still show up in Google.
What I need is a rewrite rule that redirects from "website.com" to the frontpage of "new-website.com" if the user tries to access a page that is not the frontpage and not in the web1-category.
I suppose a rewrite-rule checking for the string "web1" in the URI would be the answer to my problem, but unfortunately I have no idea how to write the syntax.
Any help would be appreciated.
Thanks in advance.
EDIT:
My htaccess file with #zessx proposed solution:
Options -Indexes
Options +FollowSymLinks
DirectoryIndex index.php
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^my-website\.com$ [NC]
RewriteRule ^(.*)$ http://www.my-website.com/$1 [L,R=301]
RewriteCond %{REQUEST_URI} !web1
RewriteRule ^(.+)$ http://www.my-new-website.com [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
This is what you need :
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} !web1
RewriteRule ^(.+)$ http://new-website.com [L,R=301]
A colleague of mine, found a solution:
RewriteCond %{REQUEST_URI} !^/web1(/|$)
RewriteCond %{REQUEST_URI} !^/admin(/|$)
RewriteCond %{REQUEST_URI} !^/$
RewriteRule ^([\w/]*)$ http://www.new-website.com [L]`
Trying to get
www.example.com
to go directly to
www.example.com/store
I have tried multiple bits of code and none work.
What I've tried:
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^(.+)\www.example\.com$
RewriteRule ^/(.*)$ /samle/%1/$1 [L]
What am I doing wrong?
You can use a rewrite rule that uses ^$ to represent the root and rewrite that to your /store directory, like this:
RewriteEngine On
RewriteRule ^$ /store [L]
I was surprised that nobody mentioned this:
RedirectMatch ^/$ /store/
Basically, it redirects the root and only the root URL.
The answer originated from this link
Try this:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
RewriteRule ^$ store [L]
If you want an external redirect (which cause the visiting browser to show the redirected URL), set the R flag there as well:
RewriteRule ^$ /store [L,R=301]
Here is what I used to redirect to a subdirectory. This did it invisibly and still allows through requests that match an existing file or whatever.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?site.com$
RewriteCond %{REQUEST_URI} !^/subdir/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /subdir/$1
RewriteCond %{HTTP_HOST} ^(www.)?site.com$
RewriteRule ^(/)?$ subdir/index.php [L]
Change out site.com and subdir with your values.
To set an invisible redirect from root to subfolder, You can use the following RewriteRule in /root/.htaccess :
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/subfolder
RewriteRule ^(.*)$ /subfolder/$1 [NC,L]
The rule above will internally redirect the browser from :
http://example.com/
to
http://example.com/subfolder
And
http://example.com/foo
to
http://example.com/subfolder/foo
while the browser will stay on the root folder.
Another alternative if you want to rewrite the URL and hide the original URL:
RewriteEngine on
RewriteRule ^(.*)$ /store/$1 [L]
With this, if you for example type http://www.example.com/product.php?id=4, it will transparently open the file at http://www.example.com/store/product.php?id=4 but without showing to the user the full url.
This seemed the simplest solution:
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/$
RewriteRule (.*) http://www.example.com/store [R=301,L]
I was getting redirect loops with some of the other solutions.
Most of the above solutions are correct but they are all missing the transparency of the redirection.
In my case, when visiting www.example.com I wanted to get redirected to the subdirectory /store but without updating the URL to www.example.com/store. (all I want is to get the page code form that directory). If that is your case the solution below works perfectly.
RewriteEngine on
RewriteCond %{HTTP_HOST} example\.com [NC]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^(.*)$ /store/$1 [L]
source: http://wiki.dreamhost.com/Transparently_redirect_your_root_directory_to_a_subdirectory
I don't understand your question...
If you want to redirect every request to a subfolder:
RewriteRule ^(.*)$ shop/$1 [L,QSA]
http://www.example.com/* -> wwwroot/store/*
If you want to redirect to a subfolder which has the domain name
RewriteCond %{HTTP_HOST} ([^\.]+\.[^\.]+)$
RewriteRule ^(.*)$ %1/$1 [L,QSA]
http://www.example.com/* -> wwwroot/example.com/*
I have found that in order to avoid circular redirection, it is important to limit the scope of redirection to root directory.
I would have used:
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/$
RewriteRule (.*) http://www.example.com/store [R=301,L]
Formerly I use the following code which is work correctly to redirect root URL of each of my domains/subdomains to their correspondence subdirectories which are named exactly as the sub/domain it self as below:
RewriteCond %{HTTP_HOST} ^sub1.domain1.com
RewriteCond %{REQUEST_URI} !subs/sub1.domain1.com/
RewriteRule ^(.*)$ subs/%{HTTP_HOST}/$1 [L,QSA]
RewriteCond %{HTTP_HOST} ^sub2.domain1.com
RewriteCond %{REQUEST_URI} !subs/sub1.domain2.com/
RewriteRule ^(.*)$ subs/%{HTTP_HOST}/$1 [L,QSA]
RewriteCond %{HTTP_HOST} ^sub1.domain2.com
RewriteCond %{REQUEST_URI} !subs/sub1.domain2.com/
RewriteRule ^(.*)$ subs/%{HTTP_HOST}/$1 [L,QSA]
RewriteCond %{HTTP_HOST} ^sub2.domain2.com
RewriteCond %{REQUEST_URI} !subs/sub2.domain2.com/
RewriteRule ^(.*)$ subs/%{HTTP_HOST}/$1 [L,QSA]
However when I want to add another subs or domains then it will need to be added in the above code. It should be much more convenient to simplify it to work like wildcard (*) as below:
RewriteCond %{HTTP_HOST} ^sub
RewriteCond %{REQUEST_URI} !/subs/
RewriteRule ^(.*)$ subs/%{HTTP_HOST}/$1 [L,QSA]
So whenever another subdomains/domains is added as long as the subdomain name has a prefix of sub (like: sub3.domain1.com, sub1.domain3.com etc.) the code will remain valid.
Two ways out of possible solutions to achieve this are:
1. Create a .htaccess file in root folder as under (just replace example.com and my_dir with your corresponding values):
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteCond %{REQUEST_URI} !^/my_dir/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /my_dir/$1
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteRule ^(/)?$ my_dir/index.php [L]
</IfModule>
Use RedirectMatch to only redirect the root URL “/” to another folder or URL,
RedirectMatch ^/$ http://www.example.com/my_dir
I think the main problems with the code you posted are:
the first line matches on a host beginning with strictly sample.com, so www.sample.com doesn't match.
the second line wants at least one character, followed by www.sample.com which also doesn't match (why did you escape the first w?)
none of the included rules redirect to the url you specified in your goal (plus, sample is misspelled as samle, but that's irrelevant).
For reference, here's the code you currently have:
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^sample.com$
RewriteRule (.*) http://www.sample.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^(.+)\www.sample\.com$
RewriteRule ^/(.*)$ /samle/%1/$1 [L]
One can use Redirect too for this purpose
Redirect 301 / www.example.com/store
Or Alias for mapping
Alias / /store
Edit: mod_alias is only applicable in httpd.conf.
Refrences
http://httpd.apache.org/docs/2.2/mod/mod_alias.html
https://httpd.apache.org/docs/trunk/rewrite/avoid.html
A little googling, gives me these results:
RewriteEngine On RewriteBase
/ RewriteRule ^index.(.*)?$
http://domain.com/subfolder/
[r=301]
This will redirect any attempt to
access a file named index.something to
your subfolder, whether the file
exists or not.
Or try this:
RewriteCond %{HTTP_HOST}
!^www.sample.com$ [NC]
RewriteRule ^(.*)$
%{HTTP_HOST}/samlse/$1 [R=301,L]
I haven't done much redirect in the .htaccess file, so I'm not sure if this will work.
try to use below lines in htaccess
Note: you may need to check what is the name of the default.html
default.html is the file that load by default in the root folder.
RewriteEngine
Redirect /default.html http://example.com/store/
you just add this code into your .htaccess file
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /folder/index.php [L]
</IfModule>
This will try the subdir if the file doesn't exist in the root. Needed this as I moved a basic .html website that expects to be ran at the root level and pushed it to a subdir. Only works if all files are flat (no .htaccess trickery in the subdir possible). Useful for linked things like css and js files.
# Internal Redirect to subdir if file is found there.
RewriteEngine on
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_URI} !-s
RewriteCond %{DOCUMENT_ROOT}/subdir/%{REQUEST_URI} -s
RewriteRule ^(.*)$ /subdir/$1 [L]
I'll answer the original question not by pointing out another possible syntax (there are many amongst the other answers) but by pointing out something I have once had to deal with, that took me a while to figure out:
What am I doing wrong?
There is a possibility that %{HTTP_HOST} is not being populated properly, or at all. Although, I've only seen that occur in only one machine on a shared host, with some custom patched apache 2.2, it's a possibility nonetheless.