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 ...
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 ...
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.
...
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’...
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}/{...
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}
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 ...
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 ...
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 ...
12
votes
Accepted
URL Format of a nested structure with 3 or more layers
Set the Nested Entries URL format to:
{parent.uri}/{slug}
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 ...
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 ...
11
votes
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 "...
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 }}">{{ ...
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 ...
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' =...
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 ...
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 ...
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 ...
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 ...
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 ...
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}
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(-...
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, ...
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 ...
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 ...
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 %{...
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 ...
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 ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
url × 271templating × 34
entries × 22
assets × 20
localization × 17
config × 17
sections × 17
routing × 17
craft3 × 16
entry × 16
routes × 16
slug × 15
siteurl × 15
structure × 14
categories × 13
plugin-development × 11
redirect × 11
htaccess × 10
entry-type × 8
image × 7
conditional × 7
request × 7
error × 6
navigation × 6
control-panel × 5