I want to use subdomain as id, and I need dynamic router to do this. In urlManager, I added this line:
"http://<user:\w+>.local.dev/<controller:\w+>/<action>" => '<controller>/<action>',
When I try any action, for example:
function actionMyAccount($user){
echo $user;...
}
I am not getting anything - the var isn't printed, and script stops working (screen is white). When I remove $user, the page is loading without any problems
How can I achieve subdomain router?
I think your router mapping setting is OK. If you want it to be more precise:
"http://<user:[^www]\w+>.local.dev/<controller:\w+>/<action:\w+>" => '<controller>/<action>'
But to make it work, you'd better double check following two things:
First, your virtual host should have a *.local.dev
server_name in nginx
or ServerAlias in Apache
Then you can use dynamic controller's name as subdomain.
Second, your virtual host should have been configured rewrite rules correctly, refer to Yii2 doc.
e.g. for Apache, just create a .htaccess file under YOUR_APP/web/ folder with following content lines:
Options +FollowSymLinks
IndexIgnore */*
RewriteEngine on
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward it to index.php
RewriteRule . index.php
Related
I wanted to put the index.php file of my website in a public file on my server, but consequently, when I tap the url, it is not directing me to the index, but to the folder which contains all of the folder of my website.
I try to put a .htaccess file in the root of the folder containing my website. It worked with my local virtual host wampserver, but not with the real host (where I put files with filezila). I show you the line code I wrote in .htaccess, which worked with wampserver, but not with the actual used server for internet ::
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^.*$ public/index.php [QSA,L]
I try also to put the absolute path but same thing. Does someone have an idea about what I have to do to config the index path of my server ? (for a subdomain) ?
I found the solution. I had to remove the first line:
Options +FollowSymlinks
My server already had it activated and didn't support the instruction twice.
I use some rewrite rules inside the Apache virtual hosts configuration httpd-vhosts.conf and it works fine there. Because I want to move to a managed server I have to use the .htaccess file because I don't have access to the apache configuration files.
I tried it locally and somehow I don't get this working at all.
Locally I use xampp 1.8.3 (Apache 2.4.10). I put the .htaccess file in the root directory of the virtual host and use this lines for testing
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule /testrule /about/about.php [L]
I always get an 404 error when calling localhost/testrule
It works fine when I put it in the httpd-vhosts.conf.
mod_rewrite is enabled and I have set AllowOverride All in the Directory part.
This blog post clearly mention
In VirtualHost context, The Pattern will initially be matched against
the part of the URL after the hostname and port, and before the query
string (e.g. “/app1/index.html”).
In Directory and htaccess context, the Pattern will initially be
matched against the filesystem path, after removing the prefix that
led the server to the current RewriteRule (e.g. “app1/index.html” or
“index.html” depending on where the directives are defined).
So in virtual host context, matches start from /testrule/...
While in .htaccess and <Directory/> context, it matches testrule/...
You can quick check the htaccess rewrite rules here:
http://htaccess.madewithlove.be/ or here:
http://martinmelin.se/rewrite-rule-tester/
Just get rid of the leading slash in the RewriteRule and you are done
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule testrule /about/about.php [L]
I'm trying to use Apache's mod_rewrite module to make dynamically generated urls from a Yii app, which always contain index.php as the router/bootstrap, into static urls through .htaccess available to browse through a web browser.
Example: localhost/projectname/index.php/commentfeed.xml is the dynamically generated page, but I want it to be able to be accessed through localhost/projectname/commentfeed.xml
Apache version I'm running is: Apache/2.4.4 (Win32) OpenSSL/1.0.1e PHP/5.5.0 through XAMPP for my development platform. The mod_rewrite module is present and loaded in Apache's httpd.conf file.
My .htaccess in localhost/projectname/ is as follows:
#Turning on the rewrite engine is necessary for the following rules and features.
#FollowSymLinks must be enabled for this to work.
<IfModule mod_rewrite.so>
Options +FollowSymlinks
RewriteEngine On
#Unless an explicit file or directory exists,
#redirect all request to Yii entry script.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
</IfModule>
Now, when I try to access commentfeed.xml through my browser without index.php (localhost/projectname/commentfeed.xml) I get a 404 error - Object not found!.
I've looked over the following guides & SO questions: URL Rewriting Guide, mod_rewrite documentation, and SO: Tips for debugging .htaccess rewrite rules but none of them had explicitly what I was looking for. Also, my apache error log is not reporting anything about the .htaccess file. Any help or guidance towards the correct usage would be greatly appreciated.
You need to tell the Yii UrlManager that the script name is not required. In your config/main.php file you should have:
'components'=>array(
...
'urlManager'=>array(
'urlFormat'=>'path',
'showScriptName'=>false,
'rules'=>array(
...
),
),
),
Make sure you also have a route defined to specify the controller/action that generates the commentfeed.xml file.
More info: http://www.yiiframework.com/doc/guide/1.1/en/topics.url#hiding-x-23x
The specific answer to my problem lay in using <IfModule> ... </IfModule>. With my setup of XAMPP v3.2.1 and the apache server installed with it, the only lines of code I needed in the .htaccess was simply:
RewriteEngine on
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward it to index.php
RewriteRule . index.php
I want to redirect a domain name : http://www.newdomain.com to http://sub.maindomain.com
I have edited .htaccess as:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !sub.maindomain.com$ [NC]
RewriteRule ^(.*)$ http://sub.maindomain.com/$1 [L,R=301]
It redirects but the http://sub.maindomain.com visible in the address bar . Help! How to do the masking so that http://www.newdomain.com would be visible . Thanks
And also I want create a subsite http://sub.newdomain.com . How to do it with .htaccess .
If www.newdomain.com and sub.maindomain.com are on different hosts, the only way to get the content on sub.maindomain.com to be served while the browser's URL address bar says www.newdomain.com is to fetch the content from sub.maindomain.com on behalf of the browser. Since as far as the browser is concerned, it's looking at content on newdomain.
You can do this either by setting up a proxy using apache or maybe a script. Using apache, you'd need access to your vhost config for the www.newdomain.com domain. You can set it up like:
ProxyPassMatch ^(.*)$ http://sub.maindomain.com$1
Take a look at the mod_proxy docs for more info on the flags you can pass this directive, to tweak timeouts and retries.
If you want to setup a php script to proxy, you'll need to route all requests to this php script, so in your htaccess file in your www.newdomain.com document root, add (assuming this php script is called proxy.php):
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/proxy.php
RewriteRule ^(.*)$ /proxy.php?url=$1 [L,QSA]
Then in proxy.php, look for the url query string, load that page, and output it to the browser.
Though all of this is much easier if you can just point both domains to the same host.
i'm hosting a website through a hosting company [1] on a linux/apache server. until now i serve the different content through one script with parameters. an example url is
www.mydomain.com/pages.php?date=1-10-2008
now i want to change the scheme the url is composed of to something which looks completely like a path url. eg.:
www.mydomain.com/pages/date/2008/20/1
for this i need to switch off the normal mapping of url paths to directory folders in apache: all requests to all paths should go to one central script (pages.php), which than analyzes the path component of the url.
how do i tweak apache for this? i hope some .htaccess rules could the trick.
[1] btw, the hosting company is godaddy.com.
Something like:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . pages.php
should rewrite every request for a file or directory that doesn't exist to pages.php. This will allow you to keep static files (images, stylesheets, etc) in the same document root.
(Shamelessly stolen from WordPress :) )
You are looking for mod_rewrite. Example htaccess file:
RewriteEngine On
RewriteBase /
RewriteRule ^pages/([^/]*)/(.*)$ pages.php?$1=$2
Rather than parse the url in your script, you should be able to handle the specific example above with Apache's ModRewrite module.
http://httpd.apache.org/docs/1.3/mod/mod_rewrite.html
You can use these in the .htaccess file, assuming your host allows this.