In this video, we'll take a look at creating sub-path aliases using the Sub-pathauto module.
First I'm going to enable the core "Contact" module, and then allow both anonymous and authenticated users to view user profiles.
Enable "Contact" module and allow all users to view user profiles
- Enable the core "Contact" module
- Go to "People => Permissions" (admin/people/permissions)
- Check the boxes next to "Use users' personal contact forms" for the "Anonymous user" and "Authenticated user" roles
- Check the boxes next to "View user profiles" for the "Anonymous user" and "Authenticated user" roles.
- Enable "Personal contact form" at "user/1/edit"
Paths without aliases
- In Firefox, as an anonymous user, click on "Brian" to view the user profile.
Here, you'll notice that the path is "users/brian", and we can see the "contact" tab. If we click it, we are taken to "user/1/contact".
Pathauto has created the URL alias "users/brian" but it does not affect other URLs that include the pattern within a larger path, so, if we tried to go to "users/brian/contact", we would get a "Page not found" error.
The Sub-pathauto module will automatically create a URL alias for every URL that is based on, or extends an existing alias.
In order to use Sub-pathauto, all we need to do is enable the module. There's virtually no configuration.
You can find Sub-pathauto at drupal.org/project/subpathauto
Install
Enable
- Sub-pathauto
Demonstrate the new path alias for contact/edit/etc.
- In Firefox, as an anonymous user, click on "Brian" to view the user profile.
The path is still "users/brian". This time, if we click the "Contact" tab, we are taken to "users/brian/contact". So, because "user/1" has been aliased to "users/brian", the Sub-pathauto module knows to also alias "user/1/contact" to "users/brian/contact"
Configure
Aliasing administration URLs
URLs like "user/1/edit" and "user/1/shortcuts" are not aliased by default. These are considered administration paths, and Sub-pathauto skips processing on these by default. We can change this behavior on the Sub-path settings page.
- Go to "Configuration => Search and metadata => URL aliases => Sub-path settings" (admin/config/search/path/subpaths)
- Uncheck the box "Skip administration paths from sub-path processing."
Now, when we go to a user profile, all of the tabs will link to aliased versions of the path, including "administration paths". (ie: the edit page.)
We have duplicate content
There's one problem with stopping our setup here. Search engines don't like it when a site has "duplicate content". Duplicate content is when the same content is available at multiple URLs. Now, I haven't purposefully created duplicate content, but because all of our content, users, etc. have aliases, they are available at multiple URLs.
For example, "Test Article ##1" is available at both: "node/1" and "blog/2012/11/test-article-1". While this is actually the exact same page, search engines will find both URLs, see them as unique URLs and penalize the site for having duplicate content. Installing the Redirect module will automatically take care of this for you.