eZ Community » Forums » General » How to start using dashes instead of...
expandshrink

How to start using dashes instead of underscores in URL?

How to start using dashes instead of underscores in URL?

Saturday 09 May 2015 6:31:13 pm - 5 replies

Hi! I have old website which has URLs like this one:

/news/old_city/who_had_happy_weekends

As I know I can change it in INI file that all new nodes will have URLs like this one:

/news/old-city/who-had-happy-weekends

It's ok for new nodes, that's what I need, but I also need to leave old URLs with underscores. Is it possible? 

Saturday 09 May 2015 11:30:27 pm

Hello Adam,

Welcome to the eZ Community!

First I would like to remind you that changes to site.ini and or transform.ini via settings overrides require the complete and successful execution of the php cli script, 'bin/php/updateniceurls.php' which will transform all the url aliases (nice urls) for call content objects within the database.

Which means that you would not be able to (by default, to my knowledge) retain the old urls with the underscores as they would be regenerated using the new url transformation rules. Since the updateniceurls script is not optional I see no default solution to the requirements you present except custom mod_rewrite rules.

It would be trivial to write a custom eZ Publish php script to generate the required mod_rewrite redirection rules required.

Here is how I would do this, create the basic eZ Publish php script internals (wrapper, argument processing, etc). This gets you started on a solid basis.

Then write script internals to generate a txt file which contains the first part of the mod rewrite rules required. Here is an example:

RewriteRule ^/news bc-node-uid-41 [R=301,L]
RewriteRule ^/news/old_city bc-node-uid-42 [R=301,L]
RewriteRule ^/news/old_city/who_had_happy_weekends bc-node-uid-43 [R=301,L] 

Notice that I have omitted the destination uri as we can not know that until the related urlalias settings have been changed and the updateniceurl script has been successfully run.

These place holders 'bc-node-uid-{nodeID}' server two functions, first as a unique identifier which can be searched for and replaced easily, second the number part of the identifier contains the nodeID which can be used to then replace the entire unique identifier+nodeID with the destination uri once the urlalias settings have been changed and the updateniceurls script has been run successfully.

The next step would be to extend the script to be run with an additional command line argument which indicates the second stage processing to be executed instead of the first stage (described above).

In the second stage script code block you would write code to iterate again over the content tree content (using the same code as in the first stage would be ideal) and then per nodeID search the txt file for the string 'bc-node-uid-'+$nodeID and replace it with the node's url alias uri (I recommend against using hostnames within your mod_rewrite rules to make them more generic and re-useable).

Here is an example of what the generated mod_rewrite rules should look like:

RewriteRule ^/news /news [R=301,L]
RewriteRule ^/news/old_city /news/old-city [R=301,L]
RewriteRule ^/news/old_city/who_had_happy_weekends /news/old-city/who-had-happy-weekends [R=301,L] 

Next follow the steps required.

  • Run the first stage part of the script to generate the first part of the txt file
  • Alter the urlalias settings as needed
  • Then run the second stage part of the script to generate the second and final part of the txt file
  • Review the txt file for apache configuration / mod_rewrite errors or other problems.
  • Install the generated mod_rewrite rules into your webserver's eZ Publish virtual host mod_rewrite configuration.
  • Test your apache configuration for errors using command: apachectl -t
  • If you have no conf errors then restart your web server
  • Test your url redirection rules by trying to access the old urls and notice that they now redirect to the new urls

Your done! Enjoy the success you have created for your website. I suggest drinking a tall glass of orange juice and relaxing outdoors happy.gif Emoticon

It's also important to remember that using [R=301,L] ensures that all search engine indexes (given time) will be updated to point the old urls to the new urls and absolutely no loss to your websites SEO or Index Rank will result from your urlalias change.

You might also want to extend the script further to remove mod_rewrite rule line entries which the origin and destination uris match as they would not be needed and would prolly cause web server mod_rewrite redirection loop server errors.

I think the script I describe could be written and tested in less than a day, depending on your experience with eZ, php and mod_rewrite. I think I could write the script in less than 3 hours (with proper testing). It would be important to test the mod_rewrite rules locally first before using them on your production website.

In short the solution I describe provides for your requirement to retain the use of the old urls / uris for existing content, while new content would require the new urls / uris. This solution does represent a migration (of sorts) and would require an eZ Publish version greater than 3.10. Their may be other possible solutions but I wager they would take more time to create and could be more complicated. 

Question: What version of eZ Publish are you using?

For those looking to learn more. The following documentation is quite informative:

https://doc.ez.no/eZ-Publish/Technical-manual/4.x/Reference/Configuration-files/site.ini/URLTranslator/WordSeparator

https://doc.ez.no/eZ-Publish/Technical-manual/4.x/Features/Multi-language-support-for-URL-aliases/URL-transformation-rules

https://github.com/ezsystems/ezpublish-legacy/blob/master/settings/site.ini#L330

https://github.com/ezsystems/ezpublish-legacy/blob/master/settings/transform.ini#L217

Please let me know your thoughts on this solution.

I hope this helps!

Cheers,
Heath

Sunday 10 May 2015 1:21:19 pm

If I remember correctly (and there are no guarantees of that), a long time ago I ran the updateniceurls.php script, changed the ini setting, cleared the ini cache, ran updateniceurls.php again and I had both _ and - urls work for the existing content.

New content, of course, would only get the second urls.  If this is something that would suit your needs, give it a try and see if it still works.  It's also possible that I hacked a script somewhere before it worked but it was a 4.x site 6-7 years ago so my memory is vague.

Sunday 10 May 2015 6:51:13 pm

Hello Adam,

Apologies for not testing the url alias separator changes before posting my first suggestion.

Per Steven's suggestion I tested this morning and found that Steven is partially right.

The old url aliases can be used -until- you edit a content object using the admin and publish a new version, at which time the old url alias redirects to the new url alias automatically.

So in a default installation the url: http://ezp5dev/Discover-eZ-Publish-5/Download-your-White-Paper-here

Is the default and valid url scheme, if you change the WordSeparator from dash to underscore and run the updateniceurls script at which point the url will be transformed into:  http://ezp5dev/Discover_eZ_Publish_5/Download_your_White_Paper_here

While the old urls remain valid (and do not redirect to the new style urls, at this point) when you edit the 'Download-your-White-Paper-here' content object and even if you make no content changes but simply re-publish the content object, then the old url alias with dashes will redirect to the underscores url alias automatically the next time you try to load the old dash url alias.

This would mean you would not (per-say) need the mod_rewrite solution I described above as eZ Publish provides for the redirection automatically.

Apologies for the confusion, It had been a while since I last used this functionality and the docs were a little unclear in this regard. I should have tested on a default installation first.

@Steven - Thank you for speaking up and sharing!

I hope this helps!

Cheers,
Heath 

Wednesday 13 May 2015 8:06:13 pm

Hello guys! Thank you both for your time! I predicted the same behaviour with redirects by apache and internal redirects by eZPublish. I just wanted to know if there is something that I don't know about eZPublish, or somebody created a special extension.

Anyway I didn't expect such descriptive posts! Thanks you both for this!

Thursday 14 May 2015 5:40:10 am

Hello Adam,

Your very welcome. We are happy to help!

I am curious what you will do as a result of this thread.

Will the default redirection features meet your needs?

Are you still looking for another solution, if so what more specifically do you need?

Best wishes

Cheers,
Heath

expandshrink

You must be logged in to post messages in this topic!

36 542 Users on board!

Forums menu

Proudly Developed with from