Here's my setup:
config.php
'urlManager'=>array(
'urlFormat'=>'path',
'rules'=>array(
'<controller:\w+>/<id:\d+>'=>'<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>'
),
'showScriptName'=>false,
),
.htaccess:
Options +FollowSymlinks
#+FollowSymLinks must be enabled for any rules to work, this is a security
#requirement of the rewrite engine. Normally it's enabled in the root and we
#shouldn't have to add it, but it doesn't hurt to do so.
RewriteEngine on
#Apache scans all incoming URL requests, checks for matches in our
#.htaccess file
#and rewrites those matching URLs to whatever we specify.
#allow blank referrers.
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?site.com [NC]
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?site.dev [NC]
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?dev.site.com [NC]
RewriteRule \.(jpg|jpeg|png|gif)$ - [NC,F,L]
# 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
On layout menus I have this:
$this->widget('zii.widgets.CMenu',
array('items'=>
array(
array(
'label'=>Yii::t('site','A'),
'url'=>array('/site/index')
),
array(
'label'=>Yii::t('site','Q'),
'url'=>array('rooms/index')
),
array(
'label'=>Yii::t('site','G'),
'url'=>array('gastronomy/index')
),
array(
'label'=>Yii::t('site','A'),
'url'=>array('activity/index')
),
array(
'label'=>Yii::t('site','S'),
'url'=>array('services/index')
),
array(
'label'=>Yii::t('site','C'),
'url'=>array('contacts/index')
),
array(
'label'=>Yii::t('site','R'),
'url'=>array('booking/index')
)
)
)
);
I explicit call index here, because it seems that, calling it explicitly is required.
With this setup, each time I click on those links I get, for example:
http://site.dev/rooms/index
While I wish to get:
http://site.dev/rooms/
W/out the index name.
What am I missing here ?
There is difference between index entry file and default action. You're messing those things.
If you make 'showScriptName'=>true you will see, that your links will be changed to something like /index.php/rooms/index where index.php is the index entry file.
As you see with option 'showScriptName'=>false you don't have that index.php in your links, that means that you successfully removed entry script from links.
Now your room/index is controller/action part of URL route. room is controller and index is action.
To see http://site.dev/rooms/ instead of http://site.dev/rooms/index you have to edit your URL routes like that:
'urlManager'=>array(
'urlFormat'=>'path',
'rules'=>array(
'<controller:\w+>/<id:\d+>'=>'<controller>/view',
'<controller:\w+>'=>'<controller>/index',
'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>'
),
'showScriptName'=>false,
),
Notice the line '<controller:\w+>'=>'<controller>/index' i added. That makes default action index to create controller route instead of controller/index.
Related
I have module in the frontend, and I want to make a friendly url like:
//yourdomain/a-zA-Z_module-controller-action-id-page-1.html
such as like this link:
http://kudufood.com/com-van-phong-lam-the-nao-de-nau-canh-bong-cai-duoc-ngon-kudu_cookbooks_1429067720.html
You can enable pretty URLs by adding the following code to your application components in config/web.php and configure custom URLs in 'rules':
'components' => [
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName'=>true,
'rules' => [
// your rules go here
'customURL'=>'my-controller/my-action'
],
If you wish to hide index.php as well, then set:
'showScriptName'=>false
and create an .htaccess file your /web directory with the following contents:
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
Documentation: http://www.yiiframework.com/doc-2.0/yii-web-urlmanager.html
I want to 301 redirect all references /en/ in a website, currently there are multple websites running on the same TYPO3 installation. For example all references would keep the same structure.
For example my-domain.com/en/ would simply go to my-domain.com, and my-domain.com/en/about-us would go to my-domain.com/about-us
Because there are multiple websites and domains running I would need to define the domain using RewriteCond. I have tried the following snippet, but no joy.
RewriteCond %{HTTP_HOST} ^my-domain.com$ [NC]
RewriteRule ^en/(.*)$ /$1 [R=301,L,NC]
What would be the correct way to achieve this?
This should do the trick:
RewriteCond %{HTTP_HOST} ^www\.domain\.com$
RewriteRule ^en/(.*)$ /$1 [L,R=301]
In addition to that configure your realurl prevars like this, to target your specific domain:
$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['realurl']['www.domain.com'] = $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['realurl']['_DEFAULT'];
$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['realurl']['www.domain.com']['preVars'] => array (
'0' => array (
'GETvar' => 'L',
'valueMap' => array (
//'en' => '0', // remove the language here
'de' => '2', // any other language...
),
'noMatch' => 'bypass' // thats the important bit
),
),
The important part on the realurl config is to keep out the default language from being integratet in the url via noMatch => bypass and removing it from the value array.
Im trying to create a 301 redirect for all pages but one directory to a new site, but Im having trouble setting it up.
This is basically what I need:
http://www.example.com/store => no redirects, users remain on http://www.example.com/store
http://www.example.com/* => all other pages go to this url http://www.newdomain.com/
AKA
http://www.example.com/apple => http://www.newdomain.com/
http://www.example.com/pie => http://www.newdomain.com/
http://www.example.com/foo/bar => http://www.newdomain.com/
I tried this method:
RewriteEngine on
RewriteCond %{REQUEST_URI}!^/store/
RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L]
But when I go to http://www.example.com/store it takes me to http://www.newdomain.com/store
Basically I need the directory /store to remain on the old domain. Can anyone help? Not to experienced with .htaccess rules...
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/store
RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L]
I have the following structure
-www
\-subfolder
In www I have my main site's index.php.
In subfolder I have a sort of admin UI and in there I'd like to have another index.php for the admin UI.
Currently my requests from within /subfolder/index.php get redirected to www/index.php and basically the pages of my admin UI don't show up.
This is my .htaccess file:
RewriteEngine On
RewriteRule ^$ index.php [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/(.*)$ index.php?lang=$1&page=$2 [L]
Can you help me? I've tried several options in other answers but as I'm no so advanced a web developer, I couldn't get any to work.
#TerryE, Sorry if I have come off as crude.
I am using a local setup for testing.
I have installed Vertrigo server, which gives me Apache server.Running on Windows7 OS. The server is installed in Program files\VertrigoServ\Apache folder.
My public folder is www. In there I have my main site definition. . The site is accessed locally via 127.0.0.1/index.php or 127.0.0.1/
I have site localization so URLs are constructed as /$lang/$page e.g. HOME
In index.php of main site I have the following:
$page = trim( ( isset( $_GET[ 'page' ] ) ? $_GET[ 'page' ] : 'home' ), '/' );
$lang = trim( ( isset( $_GET[ 'lang' ] ) ? $_GET[ 'lang' ] : 'en' ), '/' );
$langs = array( 'en', 'fr', 'ru' );
And upon this data I get to open the pages this way:
include 'html/'. $lang . '/' . $page . '.php';
All my main site's pages lie in www/html/$lang/
$_SERVER['REQUEST_URI']) gives /en/home for page HOME.
127.0.0.1/en/home WORKS
All navigation works perfectly for the main site.
However I have created an admin UI which lies in folder www/admin - one level below in www.
And in there I don't have any localization. I just have EN as language.
So at the top of the index.php in admin folder I have again
$page = trim( ( isset( $_GET[ 'page' ] ) ? $_GET[ 'page' ] : 'home' ), '/' );
However, here navigation is as follows HOME
and upon this I get to construct the pages in the index.php in admin folder as follows:
include 'html/ . $page . '.php';
the pages lie in www/admin/html
This does not work at all. Whenever I press home link in admin UI, I get redirected to my main site (non-existing page). If I add RewriteRule ^subfolder/ - [L] in .htaccess, I get HTTP 404 NOT Found error.
127.0.0.1/admin/home DOES NOT WORK. Neither does any other navigation from within admin. Thank you for your willingness and patience to help me!
I assume from this that you only have a single .htaccess file in your www directory.
Think about what the rule
RewriteRule ^(.*)/(.*)$ index.php?lang=$1&page=$2 [L]
does when interpreted in a Perdir context from www: take any URI of the form someDir/somePage and replace it by index.php?lang=someDir&page=somePage, so this will intercept and rewrite any /subfolder/index.php.
This isn't well documented but Perdir processing will preferentially use the lowest .htaccess file setting RewriteEngine On on the request path. So if you add an admin-specific .htaccess file in the "subfolder" subfolder, this will preempt the www one and circumvent this problem.
Postscript comments
Veni and other posters get in a Q&A when the real issue is one of "how do I debug my .htaccess rules if I my website is hosted by a shared service?" The reason that I add the shared service qualification is that if you have root access to your LAMP config then you can turn on Rewrite logging and the logfile at a debug level of 4-6 will give you enough forensics to work out what is going on.
However, the large majority of hobby / small service users buy their services on a shared basis and here they don't have root access and the hosting provider disables such logging for performance reasons so they have a binary feedback -- the rules work or they don't. I use a shared service and my approach (described here) is to set up a VM which mirrors this configuration for as a test and integration environment -- and in this I have such root access. However, this is probably far too complicated for most users. This is really a wider topic that merits its own Q / discussion.
On specific points:
If your structure is /(lang|admin)/page, then why do you have this rule because it can cause havoc on perdir retries.
RewriteRule ^$ index.php [QSA,L]
Better something like the following to force a redirect to a default language (assume the lang list is EN and IT in this example:
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond $1 !^/(en|it|admin)/
RewriteRule ^(.*) http://yourdomain/en/$1 [R=301,L]
Are you aware of the redirect restart /looping issues? (search for REDIRECT_STATUS)
I'm new to Stackoverflow, but not to sorting out this sort of s**t. I've got a couple of detailed articles on my blog on this.
Postscript comments -- yet more
OK some general help.
Don't us fixed IPs put an alias for 127.0.0.1 in your windows\system32\drivers\etc\hosts
You can turn on rewrite logging in your VertrigoServ Apache config and this gives you a detailed audit of where you have problems
It also helps if you have a little diagnostic stub to help you understand what is going on and this is what I came up with for index.php in the test directories:
<?php header( "Content-type: text.plain" ); echo "main: "; var_export($_GET);
But you really need for each of the following cases:
- to handle the URI exists (and stop rewrite loops)
- the defaults for / and /admin/
- admin/*
- */*
- * (and the language defaults)
and this is what I came up with. I've kept it simple. We could use complex regexps to fold some of these but why bother. You may need to add more QSAs if needed.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule . - [L]
RewriteRule ^$ index.php [QSA,L]
RewriteRule ^admin/?$ admin/index.php [QSA,L]
RewriteRule ^admin/(.*)$ admin/index.php?page=$1 [L]
RewriteRule ^(.*)/(.*)$ index.php?lang=$1&page=$2 [L]
RewriteRule ^(.*)$ index.php?lang=en&page=$1 [L]
RewriteEngine On
RewriteRule ^$ index.php [QSA,L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^subfolder/(.*)$ subfolder/index.php?page=$2 [L]
RewriteRule ^(.*)/(.*)$ index.php?lang=$1&page=$2 [L]
You could prevent requests to you subfolder from being rewritten with the change below
RewriteEngine On
RewriteRule ^$ index.php [QSA,L]
#prevent requests to your subfolder from being rewritten
RewriteCond %{REQUEST_URI} !^/subfolder/ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/(.*)$ index.php?lang=$1&page=$2 [L]
I need to alter this current re-write rule to accommodate for an admin folder. Here is my current mod-rewrite code:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z]+?)/([a-z]+?)/(.*)$ index.php?model=$1&view=$2¶ms=$3 [L,NS]
I have this folder structure:
ROOT: http://www.domain.com/
ADMIN: http://www.domain.com/admin/
If the .htaccess file is in the "admin" folder it works correctly. I get:
URL: http://domain.com/admin/faq/edit/13/asc
(NOTE: http://domain.com/admin/.htaccess)
Array
(
[model] => faq
[view] => edit
[params] => 13/asc
)
But, when its in the root folder I get:
URL: http://domain.com/admin/faq/edit/13/asc
(NOTE: http://domain.com/.htaccess)
Array
(
[model] => admin
[view] => faq
[params] => edit/13/asc
)
I want the mod-rewrite to recognize that admin folder and is an actual directory and use a separate re-write rule.
Thanks in advance.
Add
RewriteCond ^admin/
RewriteRule (your Rule) [L]
before the other condition. That should do the trick.
Another way would be to include the condition in the rule directly via
RewriteRule ^admin/(rest_of_regex)$ (regex_stuff) [L]
Add this to your root .htaccess:
RewriteCond ^admin/
RewriteRule (whatever rule you need for the admin folder) [L]
Add this right after the RewriteEngine On. The key point here is [L] which tells mod-rewrite to stop executing the rest of the rules if this one matches. If you need more than one rule for the admin folder, just add more RewriteRule statements, but remember to only add the [L] to the last one.
That should do it.