Hot answers tagged

46

As of Craft 2, you should be using the new siteUrl config setting. There are two advantages to setting your site URL using this config setting. First, The value can be set on a per-locale basis, in a way where Craft is actually aware of all locales’ URLs on every request (unlike the CRAFT_SITE_URL constant, where Craft only knows the current locale’s URL). ...


32

This isn't documented, so it's subject to change: When you set a Single to be the homepage, its slug is automatically set to __home__. If you want your homepage to be part of a Structure, rather than a separate Single, you can set the slug of a Structure entry to __home__, which makes it act as the site's homepage and gives it that little Home icon in ...


21

That’s what getUrl() returns if the transform hasn’t been generated yet. When the browser requests that URL, Craft will generate the transform, and respond with a 302 redirect to the actual image URL. Update: As of Craft 2.2, even if the transform generation URL doesn’t get directly requested (e.g. if it’s a social media image, or a srcset size that doesn’t ...


20

The issue here is that your Redirect directive is conflicting with Craft’s index.php-hiding RewriteRule directive. (Redirect is a mod_alias directive and RewriteRule is a mod_rewrite directive. They’re two separate modules that can be used for the same thing, but lead to unexpected consequences when used together.) To fix this, use a RewriteRule directive ...


17

If you go into your Category Group’s settings and set the following settings: Craft 2 Top Level URL Format: {slug} Nested URL Format: {parent.uri}/{slug} Craft 3 Category URI Format: {parent.uri}/{slug} then you would be able to set your section’s Entry URL Format to this: Craft 2 {myCategoryField.last.uri}/{slug} Craft 3 {myCategoryField.inReverse()...


16

This got it working for me: /section/{categoryFieldName.first.slug}/{slug}


15

I bet this trips up a lot of people at first, but your asset field will return a ElementCriteriaModel, not your image. To get to image, you need to fetch it. If your field has only one image, you will do something like this: {# Fetches the first image #} {% set image = entry.featureImage.first() %} {# It's good to check if you actually have an image before ...


14

The "this is for the homepage" button available on single sections does two things for you, when you navigate to the home page's URL: it populates the entry variable with the single's entry model it loads the template spezified in the single's CP settings . Actually nothing you can't do manually with your home page's index.html: {# Replace the default `...


12

One method is using something like the following in your general.php file. Using this method the config settings will take on the scheme of the request being made. I've set both the siteUrl and example environmentVariables below: <?php // Ensure our urls have the right scheme define('URI_SCHEME', ( isset($_SERVER['HTTPS'] ) ) ? "https://" : "http://" ...


11

It doesn’t matter to Craft, but generally I think it’s better to have consistent URLs, so they can be discoverable. For example if you land on /places/place-name, it would be reasonable to assume that you could chop off “place-name” and find an index of all the places at /places/.


11

I was about to ask this question, but realized that it's closely related to this question. The answer, like in the above referenced question lies with Twig filters. Specifically, in this case, one needs to use the url_encode filter to convert the text needed. Example <a href="http://www.linkedin.com/shareArticle?mini=true&url={{ entry.url|...


11

Sure just do this discography/{type}/{slug}


10

You could just set your "Entry URL Format" to: my-section/{type.name}/{slug} Your entry type names couldn't be as clean when composing entries in the CP as you'd need to set them to something like "example-entry-type", but it would work. Now for the really clean way: Install the Low Regex for Craft plugin. Set "Entry URL Format" to: my-section/{type....


9

It actually used to work this way when Craft first launched, but we had feedback where it was desired behavior to allow accented characters and even multi-byte characters in slugs, because they are perfectly valid in the URI. e.g. http://domain.com/會意字 However, enough people have brought it up where they would like the old behavior that we're going to add ...


9

Set the Nested Entries URL format to: {parent.uri}/{slug}


9

I solved the problem and am writing it down for future people encountering the same problem. Add the following configuration to the your craft/config/general.php config file: 'omitScriptNameInUrls' => true URLs will start to work fine.


9

The shortest way, still checking if the parent exists, is to use (not tested): {{ entry.parent ? entry.parent.link }} entry.link, or entry.getLink() gives you the <a href="{{ entry.url }}">{{ entry.title }}</a> part.


9

In most Craft projects I do I'll create a pagination include file so anything I need that requires pagination, I can just include that and I get the same pagination pattern anywhere within my site when I need it. While you do have to set the country attribute with each link, sometimes it might get a little tedious to do this manually or you may expand your ...


8

There are multiple ways to set the URLs to your files. I don't know if I got your folder structure right. But let's suppose that your web root is set to /var/www/craft-portfolio/public/, your CSS folder is /var/www/craft-portfolio/public/css/ and your Craft folder (actually not relevant) is /var/www/craft-portfolio/craft/. Root-relative URL Root-relative ...


8

Alright, I was able to figure it out myself: channel{% if object.type == 'xyz' %}/{type}{% endif %}/{slug} produces the desired results. The section for Dynamic Entry Titles in the docs pointed me into the right direction.


8

You're close! For the month part you need postDate|date("m") (this page has the php date formats). news/{postDate|date("Y")}/{postDate|date("m")}/{slug}


7

It is possible to have the query pick a random entry. From the Craft Entries Fields docs we see that entry.entriesFieldHandle is a shortcut for craft.entries({ relatedTo: { sourceElement: entry, field: "entriesFieldHandle" }, order: "sortOrder", limit: null }) And what we want is to order the entries randomly, and then limit the number we retrieve to ...


7

I've successfully used this entry url format and it works great: {{ craft.entries.id(65).first.uri }}/{slug} I would advise using the entry id over a slug, as it will never change. You also don't need to specify the section if you've included the id. For a multi-lingual site, you can specify the locale for each entry url format: {{ craft.entries.id(65)....


7

This isn't possible because the #hash-part isn't even send to the server. See this question on StackOverflow: Can PHP read the hash portion of the URL? But if you use ?hash=something instead, these functions will help you: getParam() and getQuery()


7

On recent Ubuntu systems (for sure on 14.04), mod_rewrite is installed but not enabled within Apache. You may just need to enable the rewrite module and restart Apache. $ sudo a2enmod rewrite $ sudo service apache2 restart


7

Taken from this answer on Stack Overflow: .htaccess mod_rewrite: rewriting querystring to path <IfModule mod_rewrite.c> RewriteEngine On # Remove index.php RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC] RewriteCond %{REQUEST_URI} !^/index\.php/admin\b [NC] # Don't mess with admin requests RewriteRule (.*?)index\.php/*(.*) /$1$2 ...


6

One way you can avoid this is by making a small change to the htaccess rules for removing index.php from Craft URLs. You can remove the query string from RewriteRule (.+) index.php?p=$1 [QSA,L] and replace it with /$1 So the full set of rules to remove index.php would be # Send would-be 404 requests to Craft RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %...


6

My two cents: I wouldn't use different url's for the same product, that's considered bad seo. Instead use something like domain.com/product/slug. That way you can change product categories while keeping the links intact, and it'll be easier to get the url of entries. If there is no specific reason for using structures, use native categories. Instead of ...


6

You're options are: Adding it as a parameter in the querystring (e.g. domain.com?key1=value1) Submitting it though a form and grabbing the value from POST. Setting the value in a cookie and retrieving it on the next page using something like this plugin. Writing a plugin with a Twig function that wraps Craft's craft()->userSession->setFlash() method, ...


6

The url's are always bound to entries. And with your current code, you're getting all entries in the 'contact' section, not the section itself. But if this is a single, what you want is the url of the entry in the 'contact' section. Try this: {% set contactPage = craft.entries.section('contact').first() %} {{ contactPage.getUrl() }}


Only top voted, non community-wiki answers of a minimum length are eligible