Need to hide the ID number in SEF URLs. Use RewriteRule?

Discuss Search Engine Optimization in relation to Joomla! 2.5. This forum will also have discussions on SEF/SEO Joomla! 2.5 extensions.

Moderator: General Support Moderators

Forum rules
Forum Rules
Absolute Beginner's Guide to Joomla! <-- please read before posting, this means YOU.
Forum Post Assistant - If you are serious about wanting help, you will use this tool to help you post.
Dave Everitt
Joomla! Apprentice
Joomla! Apprentice
Posts: 6
Joined: Sat Mar 19, 2011 2:44 pm

Need to hide the ID number in SEF URLs. Use RewriteRule?

Postby Dave Everitt » Sat Mar 19, 2011 3:11 pm

have a series of sequentially-numbered items under several categories, which appear as numbered lists on this page. I am going to link each one to a separate page. However, the added ID in the Joomla 1.6 URL is confusing the client, since each item already has a unique number (hence no danger of URL clashes). I know that Joomla 1.6 adds an ID even to SEF URLs, but...

I need this: http://vision2020.org.uk/wishlist-for-retail/131-16-local-shops.html

to become: http://vision2020.org.uk/wishlist-for-retail/16-local-shops.html

Since .htaccess seems the most efficient way of approaching this, I've tried adding a RewriteRule rule in .htaccess (with no rewriteCond, or a very literal one for testing: RewriteCond %{HTTP_HOST} ^/wishlist-for-retail/131-16-local-shops.html$) with no effect:

Code: Select all

RewriteRule ^/([\w-]+)/([\d]+[-])([\d\w-]+).html$ /$1/$3.html [R=301,L]

Has anyone tackled this issue successfully? Or can someone help with the RewriteRule? BTW I don't want the overhead of JoomSEF (the only one that works with 1.6). BTW these links will only ever be used in a tags wrapped around the list items on the main list page - they will never appear anywhere else.

g1smd
Joomla! Guru
Joomla! Guru
Posts: 951
Joined: Mon Feb 21, 2011 4:02 pm
Location: UK

Re: Need to hide the ID number in SEF URLs. Use RewriteRule?

Postby g1smd » Sun Mar 20, 2011 9:45 pm

Code: Select all

I need this: http://vision2020.org.uk/wishlist-for-retail/131-16-local-shops.html
to become: http://vision2020.org.uk/wishlist-for-retail/16-local-shops.html
In order for it to "become" that URL, you will have to link to http://vision2020.org.uk/wishlist-for-retail/16-local-shops.html from within the pages of your site. It is links that define URLs.

You then need an internal Rewrite to accept requests for that URL and fetch the content from inside the server using the internal server filepath and filename targeted by the RewriteRule.

Your example code is for an external Redirect. That's the final part of the puzzle. If clients request the "old" URL by clicking on old bookmarks, links found on other sites or presented in stale SERPs, your code tells the browser to make a new request for a different URL.

Your site must link to the new URLs. Clicking any link in the internal site navigation must NOT result in a redirect.

Be aware that the RewriteRule RegEx pattern can "see" only the path part of the request. Likewise the HTTP_HOST variable holds ONLY the domain name, and QUERY_STRING holds ONLY the query string parameters.

Mod_rewrite deals with requests arriving at the server after a link is clicked. Mod_rewrite cannot and does not change the URLs mentioned in the links on your pages.
Online since 1995.

User avatar
RussW
Joomla! Exemplar
Joomla! Exemplar
Posts: 9350
Joined: Sun Oct 22, 2006 4:42 am
Location: Sunshine Coast, Queensland, Australia
Contact:

Re: Need to hide the ID number in SEF URLs. Use RewriteRule?

Postby RussW » Sun Mar 20, 2011 11:15 pm

Try SEF404 Component, I beleive this allows you to not use the ID in UEL's.
Joomla! on the fabulous Sunshine Coast...
hotmango, web & print http://www.hotmango.me/
Network SMARTS, Systems Engineering http://www.networksmarts.com.au/

User avatar
betweenbrain
Joomla! Guru
Joomla! Guru
Posts: 800
Joined: Wed Feb 28, 2007 5:40 am
Location: Connecticut, USA
Contact:

Re: Need to hide the ID number in SEF URLs. Use RewriteRule?

Postby betweenbrain » Sun Mar 20, 2011 11:20 pm

I'd recommend the HP Router plugin http://www.joomlager.de/hp-router as it was designed exactly for this purpose.

Best,

Matt
betweenbrain™ | http://www.betweenbrain.com
Construct Template Development Framework | http://construct-framework.com/

Dave Everitt
Joomla! Apprentice
Joomla! Apprentice
Posts: 6
Joined: Sat Mar 19, 2011 2:44 pm

Re: Need to hide the ID number in SEF URLs. Use RewriteRule?

Postby Dave Everitt » Mon Mar 21, 2011 9:21 am

@RussW SEF404 isn't 1.6-friendly yet... and I wanted to avoid the overhead of a plugin, but...
@betweenbrain HP Router is compatible with 1.6, I'll try that if this fails.

@g1smd thanks, a useful reply. By 'an internal Rewrite to accept requests for that URL' do you mean within Joomla? Apart from that, I now see some of the problems:

1. I omitted the initial ? from the pattern. Duh#1.

2. The pattern matched the destination URL (with the ID), instead of the incoming 'non-ID' URL. Duh#2 - that's backwards, so...

3. ...effectively I need this, which works fine (unless I add a \d pattern):

Code: Select all

RewriteRule ^/?([\w-]+)/16-([\w-]+)\.html$ http://vision2020.org.uk/$1/131-16-$2.html

4. but now I don't have the ID for the destination part of the redirect because there's no ID in the incoming URL, so...

Question: is there any way to capture the ID of working destination links that match a RewriteCond pattern, then pass this captured ID variable to the second part of the RewriteRule?

g1smd
Joomla! Guru
Joomla! Guru
Posts: 951
Joined: Mon Feb 21, 2011 4:02 pm
Location: UK

Re: Need to hide the ID number in SEF URLs. Use RewriteRule?

Postby g1smd » Mon Mar 21, 2011 9:30 am

RewriteRule can deal only with what is in the request coming from the browser. It cannot add things unless you physically code those into the rule.

Your latest code is for a 302 redirect because you have included the domain name in the rule target. That rule will change the URL shown in the browser URL bar when the redirect forces the browser to make a new request for that new URL.

You now have a Duplicate Content issue (two URLs with the same content) and if you're not careful you'll also have an infinite redirect loop.

Old URL: http://vision2020.org.uk/wishlist-for-retail/131-16-local-shops.html
New URL: http://vision2020.org.uk/wishlist-for-retail/16-local-shops.html
Internal filepath: /index.php?option=com_content&view=article&id=984&Itemid=2564

You need an external redirect from http://vision2020.org.uk/wishlist-for-retail/131-16-local-shops.html to http://vision2020.org.uk/wishlist-for-retail/16-local-shops.html

You need to link to http://vision2020.org.uk/wishlist-for-retail/16-local-shops.html

You need an internal rewrite from http://vision2020.org.uk/wishlist-for-retail/16-local-shops.html to /index.php?option=com_content&view=article&id=984&Itemid=2564 (or whatever the real internal server path and file is).

URLs are a reference system used "out there" on the web. Server filepaths and filenames are a reference system used "in here" inside the server. The two are NOT related. They are merely "associated" by the actions of a server. The server responds to URL requests sent by a browser after a link is clicked and fetches content from inside the server.

The incoming request is first intercepted by mod_rewrite. It does some pattern matching with the request, comparing it to those found in your RewriteRules. It then either sends a redirect response back to the browser informing it that it needs to make a new request for a different URL, OR it internally rewrites the request to fetch content from a non-default location i.e. an internal location that is different to the one suggested by the path part of the original URL request, OR mod_rewrite performs no actions on the request and passes it to the default Apache handlers to fetch the content from the filesystem using the originally requested path and filename information.

That's it!

It is important that you understand the difference between an external redirect and an internal rewrite. You also need to be clear that URLs and server filepaths are not at all the same thing. Finally, mod_rewrite cannot "change" URLs. URLs are defined by the links on your pages. Once you click the link, that *IS* the URL.
Online since 1995.

Dave Everitt
Joomla! Apprentice
Joomla! Apprentice
Posts: 6
Joined: Sat Mar 19, 2011 2:44 pm

Re: Need to hide the ID number in SEF URLs. Use RewriteRule?

Postby Dave Everitt » Mon Mar 21, 2011 10:52 am

Thanks for your patience on this - I want to understand it properly. BTW I'm using [DOMAIN] to shorten the post - it's not in the code!

g1smd wrote:Your latest code is for a 302 redirect because you have included the domain name in the rule target. That rule will change the URL shown in the browser URL bar when the redirect forces the browser to make a new request for that new URL.

Yes, I get that (BTW now using / instead of the domain).

g1smd wrote:You now have [...] two URLs with the same content and if you're not careful you'll also have an infinite redirect loop.

Yes, that happened when I added a \d pattern for the integers and left the '131-' part in the destination.

g1smd wrote:You need an external redirect from [DOMAIN]/wishlist-for-retail/131-16-local-shops.html to [DOMAIN]/wishlist-for-retail/16-local-shops.html

Understood - that's my original pattern (with some corrections).

g1smd wrote:You need to link to [DOMAIN]/wishlist-for-retail/16-local-shops.html

Yes, am doing that. The part I don't yet get is the internal rewrite...

g1smd wrote:You need an internal rewrite from [DOMAIN]/wishlist-for-retail/16-local-shops.html to /index.php?option=com_content&view=article&id=984&Itemid=2564 (or whatever the real internal server path and file is).

So (apologies for my pedantic understanding but I want to get this right - I have 139 similar URLs) - I need to:

1. have a link on the site to [DOMAIN]/wishlist-for-retail/16-local-shops.html (understood, done)
2. external redirect [DOMAIN]/wishlist-for-retail/131-16-local-shops.html to the above (understood, can do for all 139 links with a pattern match)
3. internal rewrite [DOMAIN]/wishlist-for-retail/16-local-shops.html to get content from (e.g.) /index.php?option=com_content&view=article&id=984&Itemid=131 (not fully understood!)

Dumb but necessary question: for the internal rewrite at point [3] above, do I use another RewriteRule? If so, for a generic rule that matches multiple instances I first need the article and item ID (131 from point [2] above) so I can do e.g.: /index.php?option=com_content&view=article&id=$1&Itemid=$2

g1smd
Joomla! Guru
Joomla! Guru
Posts: 951
Joined: Mon Feb 21, 2011 4:02 pm
Location: UK

Re: Need to hide the ID number in SEF URLs. Use RewriteRule?

Postby g1smd » Mon Mar 21, 2011 11:12 am

The rewrite "translates" the incoming URL request into a silent internal server filesystem fetch. The rewrite does not let the outside world know what the internal filesystem location is (unlike a redirect which tells the browser to go fetch a different URL).

You can populate the $1 and $2 values only if that information is included within the URL request or you hard code the values in the rule itself.

That is, you could rewrite a URL request for www.example.com/pets/cats/siamese to the internal server filepath /index.php?category=pets&type=cats&breed=siamese using a rule like

Code: Select all

RewriteRule ^([^/]+)/([^/]+)/([^.]+)$ /index.php?category=$1&type=$2&breed=$3 [L]


However, for the same server filepath /index.php?category=pets&type=cats&breed=siamese and a URL like www.example.com/siamese the RewriteRule would have to hard-code some parameter values because mod_rewrite has no way to know to add the "pets" and "cats" parameter values as they are not contained within the URL request it is servicing.

Code: Select all

RewriteRule ^([^.]+)$ /index.php?category=pets&type=cats&breed=$1 [L]
This latter option is the one you will need to investigate.

Your ruleset therefore consists of a RewriteRule with [R=301,L] flags to do the redirecting and another RewriteRule with the [L] flag to do the internal rewriting.

Each rule may handle one explicit URL (in which case you will need a lot of rules), or it may use pattern matching to handle multiple URLs with similar format (in which case you will have a small number of rules).
Last edited by g1smd on Mon Mar 21, 2011 11:22 am, edited 3 times in total.
Online since 1995.

Dave Everitt
Joomla! Apprentice
Joomla! Apprentice
Posts: 6
Joined: Sat Mar 19, 2011 2:44 pm

Re: Need to hide the ID number in SEF URLs. Use RewriteRule?

Postby Dave Everitt » Mon Mar 21, 2011 11:18 am

So, in my case it can't be done. Okay, thanks :-)

g1smd
Joomla! Guru
Joomla! Guru
Posts: 951
Joined: Mon Feb 21, 2011 4:02 pm
Location: UK

Re: Need to hide the ID number in SEF URLs. Use RewriteRule?

Postby g1smd » Mon Mar 21, 2011 11:25 am

It can be done, but you'll have one rule for each URL that needs rewriting.

You can either add those rules to your .htaccess file, or find an extension or plug-in that can manage the work for you.
Online since 1995.

Dave Everitt
Joomla! Apprentice
Joomla! Apprentice
Posts: 6
Joined: Sat Mar 19, 2011 2:44 pm

Re: Need to hide the ID number in SEF URLs. Use RewriteRule?

Postby Dave Everitt » Mon Mar 21, 2011 8:52 pm

Thanks for all the help. Unless I've misunderstood things, seems I can (not sure, still reading docs??) capture the ID in a variable from a RewriteCond pattern match for (e.g.) /index.php?option=com_content&view=article&id=984&Itemid=131 placed immediately before the internal RewriteRule at point [3], so that's the next step to try.

If not, writing hundreds of almost-identical rules with a hard-coded ID seems pointless and very un-DRY, so I'd try HP Router. At least I now know enough to use mod_rewrite on other sites :-)


Return to “Search Engine Optimization (Joomla! SEO) in Joomla! 2.5”

Who is online

Users browsing this forum: No registered users and 7 guests