I want to redirect a url in my lcalhost ,as when user types : mystring.localhost/CI to localhost/CI/Mycontroller/Myaction/mystring in codeigniter .its now showing Server not found and says browser Firefox can't find the server at www.mystring.localhost. How to do this ? Thanks in advance. I have this in my .htaccess :
RewriteEngine On
RewriteCond %{HTTP_HOST} ^mystring\.localhost\CI\
RewriteRule ^(.*)$ http://localhost/CI/Mycontroller/myaction/mystring/$1 [R=301]
On Windows edit the file C:\Windows\System32\drivers\etc\hosts and then append:
127.0.0.1 mystring.localhost
So it will redirect all requests from mystring.localhost to 127.0.0.1
On Linux is the same syntax but file is at /etc/hosts
And then, you must make some Alias or RewriteRule with RewriteCond on Apache configuration or .htaccess.
add to c:\wamp\bin\apache\ApacheYOUVERSION\conf\extra\httpd-vhosts.conf
NameVirtualHost *.yourdomain.dev
<VirtualHost *:80>
DocumentRoot "c:/wamp/www/CI"
ServerName *.yourdomain.dev
</VirtualHost>
Find #Include conf/extra/httpd-vhosts.conf in c:\wamp\bin\apache\ApacheYOUVERSION\conf\httpd.conf and delete # at the beginning
in your hosts file: 127.0.0.1 yourdomain.dev
add this to your htaccess:
RewriteCond %{HTTP_HOST} ^([a-z0-9-]+).yourdomain.dev [NC]
RewriteRule ([a-z0-9-]+)(/+.*) index.php/$1/%1$2 [QSA]
So if I understand correctly:
-Someone would type in mystring.localhost/CI, and you would like it to redirect them to a certain page with an action and value?
If so, you can set a page as the index page in \application\config\config.php
Its on line 30:
$config['index_page'] = 'Mycontroller/Myaction/mystring';
Otherwise in the controller that handles your index just do a redirect in it's __construct function.
There is most likely a solution involving the HTACCESS file but I am not very knowledgeable on that.
Hope this helps!
-Gui
Related
I am newby in htaccess, please help if you can.
My task is simple, I think.
I have a website, for example newsite.com. It was made on wordpress.
And I have old copy of this site, on other domain, for example oldsite.com.
When I transfered files from oldsite to newsite some of pictures didn't transferred to newsite, but they still exists on oldsite.
Now when pages are loading I checking post thumbnails on newsite, and if they don't exists on newsite, I am replacing newsite url to oldsite url.
But is there any ways to not just replace newsite.com/image.jpg to oldsite.com/image.jpg, but replace newsite.com/image.jpg, to, for example newsite.com/imgs/image.jpg, and in .htaccess file make rule that if url has /imgs/ part, it means that need to look for image in oldsite.com/image.jpg
I think you can do it by below .htaccess code
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^OLDDOMAIN\.com$ [NC]
RewriteRule ^(.*)$ http://NEWDOMAIN.com [R=301,L]
In your new site HTML code
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ([^.]+\.(jpe?g|gif|bmp|png))$ http://www.newurl.com/imgs/$1 [R=301,L,NC]
You can put this configuration in the file of virtualhost.
<VirtualHost *:80>
ServerName example.org
DocumentRoot /srv/redireccion
Redirect permanent / https://example2.org/
</VirtualHost>
I have different domains say like (fictious names) "plumbers.org" and "cleaning.org"
pointing to the same folder in my apache2 conf.
and the main domain is "workers.org"
what i want to obtain is basically to have plumbers.org pointing to workers.org/index.php?version=1
and to have cleaning.org pointing to workers.org/index.php?version=2
is something I set in my .htacess? what's best practice?
You can try to add a permanent redirect in the original httpd / apache conf file, e.g.:
<VirtualHost ...>
ServerName plumbers.org
Redirect 301 / http://workers.org/index.php?version=1
</VirtualHost>
...
<VirtualHost ...>
ServerName cleaning.org
Redirect 301 / http://workers.org/index.php?version=2
</VirtualHost>
Technically, it should also work if you put these changes in the corresponding .htaccess files (if .htaccess files are not prohibited by the "parent" conf). Without <VirtualHost> section, of course, then.
Assuming all 3 domains point to the same DocumentRoot folder, you can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?:www\.)plumbers\.org$ [NC]
RewriteRule ^/?$ index.php?version=1 [L,QSA]
RewriteCond %{HTTP_HOST} ^(?:www\.)cleaning\.org$ [NC]
RewriteRule ^/?$ index.php?version=2 [L,QSA]
I started a php project based on http://www.php-mvc.net/ and i searched on Google how to set up my WAMP Server to enable URL rewriting.
I'm running the latest version of WAMP.
1) Enable mod_rewrite module on httpd.conf
2) On httpd-vhosts.conf i added a virtual host
<VirtualHost *:80>
ServerName phpmvc.test
ServerAlias *.phpmvc.test
VirtualDocumentRoot "C:\wamp\www\phpmvc"
ErrorLog "logs\errors.log"
<directory "C:\wamp\www\phpmvc">
Options Indexes FollowSymLinks
AllowOverride all
Order Deny,Allow
Deny from all
Allow from all
</directory>
3) On my hosts file i added the host 127.0.0.1 phpmvc.test
4) On .htacces file i have this lines
RewriteEngine on
RewriteRule ^(.*)\/$ index.php?url=$1 [QSA,L]
I restarted Apache after setting up the files and when i went to phpmvc.test it worked fine but when i tried phpmvc.test/test it shows an error that says that the requested URL is not found on this server.
EDIT: I tried with RewriteRule ^(.*)$ index.php?url=$1 [QSA,L] but it shows the same error
EDIT2: I tried with EDIT: I tried with Rahil Wazir suggestion but it stills not working.
I have this controller file IndexController.class.php and it works if i access with this URL: phpmvc.test?url=index but if i try phpmvc.test/index it says that index is not found on the server.
<?php
class IndexController {
public static function index() {
echo "Index controller";
}
}
Printing $_GET and $_SERVER on root i get an empty Array() and the server array. If i go to phpmvc.test/index?url=index i get the value on $_GET["url"] and on $_SERVER [QUERY_STRING] => url=index.
This is because Apache thinks /test is a file or directory which you are trying to accessing which probably don't exist.
You need to apply those two famous condition which is used by most Front controller pattern frameworks:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
Which tells Apache Rewrite Module unless the file or directory from the requested uri is exist then apply the rewrite rule, otherwise render the contents of the file or directory.
Debug by checking what you are getting in $_GET and $_SERVER variables.
print_r($_GET);
print_r($_SERVER);
Compare the differences in the values for ['url'] index.
Also, it may depend on your sub-folder depth as seen on the URL before /index.
i just want to ask a quick question about .htaccess.
Here's how my webhosting works with subdomains...
Once i create a subdomain... then they create a folder into the root folder like this...
www.mydomain.com ---> public_html
sub.mydomain.com ---> public_html/sub
What i want to do is... to redirect all request from sub.mydomain.com to www.mydomain.com with some GET variable or something to identify from what subdomain the request is coming from...
So for example... when i get a requests to work like this
http://sub.mydomain.com/myphp.php ---> http://www.mydomain.com/myphp.php?comingfrom=sub
http://sub.mydomain.com/(anyUrl) ---> http://www.mydomain.com/(anyUrl)?comingfrom=sub
I'm also wondering if this would execute some .htaccess redirects present in the main domain...
Hope you guys could help me...
Thanks in advance...
Put a .htaccess with following content into your subdomain folders:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.*)\.mydomain\.com$ [NC]
RewriteRule ^(.*) http://www.mydomain.com$1?comingfrom=%1 [QSA,R=301,L]
(untested, sorry)
EDIT:
you pointed out that you would like to keep the subdomain in your addressbar and don't want a redirect. So you need to make a view changes to your <VirtualHost> of www.mydomain.com like so
<VirtualHost ...:80>
ServerName www.mydomain.com
ServerAlias mydomain.com
ServerAlias sub.mydomain.com
DocumentRoot /path/to/your/docroot/of/www.mydomain.com
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.mydomain\.com [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.mydomain\.com$ [NC]
RewriteRule ^(.*) $1?comingfrom=%1 [QSA,PT,L]
</VirtualHost>
How to serve contents from different directory for virtual sub domains.
Ex: http://www.website.com gets its content from www directory.
for virtual.company.com i want to serve content from /www/application directory.
And i want to keep the url parameter same.
I have already created php files to display custom message based on the sub-domain., But i am having problems with htaccess,
In simple i just want to load content for subdomains from different directory other than the root directory.
Try this:
RewriteCond %{HTTP_HOST} !^www\.company\.com
RewriteCond %{HTTP_HOST} ^([^.]+)\.company\.com$ [NC]
RewriteRule ^(.*)$ /application/%1 [QSA,L]
That doesn´t sound like a very good idea as people could enter http://www.website.com/application and that might lead to unwanted results. I would recommend setting up a virtual host for virtual.company.com and separate the two.
However, it is possible and it would be something like:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^virtual\.company\.com$ [NC]
RewriteRule ^(.*)$ http://www.company.com/application/$1 [R=301,L]
Setup a named virtual host.
<VirtualHost *:80>
ServerName sub.domain.com
DocumentRoot "/path/to/sub/domain"
</VirtualHost>
<VirtualHost *:80>
ServerName another.domain.com
DocumentRoot "/path/to/another/sub/domain"
</VirtualHost>