Hot answers tagged

47 votes
Accepted

What’s the recommended way to set the site URL?

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 ...
user avatar
32 votes

Add home page to a structure?

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 ...
user avatar
  • 3,400
21 votes
Accepted

Image transforms returning cpresources url

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. ...
user avatar
20 votes
Accepted

301 Redirects Appends ?p=original/url/path - how can I remove?

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’...
user avatar
  • 2,455
18 votes
Accepted

How do I add a subcategory to the URL slug for a channel entry?

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}/{...
user avatar
16 votes
Accepted

How would I include the first category slug in the section's entry URL format?

This got it working for me: /section/{categoryFieldName.first.slug}/{slug}
user avatar
  • 1,661
16 votes
Accepted

Fetch Image URL from entry CMS

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 ...
user avatar
  • 2,957
14 votes
Accepted

Add home page to a structure?

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 ...
user avatar
  • 35.8k
13 votes
Accepted

How can I convert text into a URL-friendly string?

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 ...
user avatar
12 votes
Accepted

URL Format of a nested structure with 3 or more layers

Set the Nested Entries URL format to: {parent.uri}/{slug}
user avatar
  • 6,226
12 votes

How to set up {siteUrl} to work with both http and https

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 ...
user avatar
  • 13.2k
11 votes
Accepted

Should I pluralize a channel's index slug?

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 ...
user avatar
11 votes

How do I add Entry Types to URL?

Sure just do this discography/{type}/{slug}
user avatar
10 votes
Accepted

Entry Types Custom Slug

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 "...
user avatar
  • 2,985
10 votes
Accepted

Is there a tag to get the parents url like {{entry.url}}?

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 }}">{{ ...
user avatar
  • 6,288
9 votes
Accepted

How automatic slug generator could replace accentuated letters?

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 ...
user avatar
  • 66.4k
9 votes

URL rewrite not working

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' =...
user avatar
9 votes

Using pagination on a page with url with query string data

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 ...
user avatar
  • 1,022
8 votes
Accepted

CSS is not loading on staging server

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 ...
user avatar
  • 35.8k
8 votes
Accepted

Method to grab URL hash in Twig or craft.request?

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 ...
user avatar
  • 8,316
8 votes
Accepted

Using an IF-statement to define the Entry URL Format

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 ...
user avatar
  • 336
8 votes
Accepted

How do I get the url of single pages?

Craft creates an EntryModel for each Single Section, that allows you to access the URL property you are after. You can get the (one) EntryModel using the Section's ID and the first() method: {% set ...
user avatar
  • 35.8k
8 votes
Accepted

How to add year and month to Entry URL Format

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}
user avatar
8 votes
Accepted

How to get second last url segment

You can use negative numbers as the parameter for segment which then counts from right to left. So {{ craft.app.request.segment(-1) }} will get the last segment. And... {{ craft.app.request.segment(-...
user avatar
  • 1,979
7 votes
Accepted

How do I get a random entry's URL from a Entries field?

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, ...
user avatar
7 votes

Dynamically setting a channel's Entry URL Format with Craft tags

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 ...
user avatar
  • 6,226
7 votes
Accepted

omitScriptNameInUrls is not working

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 ...
user avatar
7 votes
Accepted

How to remove index.php?p= permanently?

Taken from this answer on Stack Overflow: .htaccess mod_rewrite: rewriting querystring to path <IfModule mod_rewrite.c> RewriteEngine On # Remove index.php RewriteCond %{...
user avatar
  • 2,573
6 votes

301 Redirects Appends ?p=original/url/path - how can I remove?

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 ...
user avatar
6 votes

Best approach for building a webshop with more than one URL to the same product (entry)

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 ...
user avatar
  • 8,316

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