Welcome Guest, Not a member yet? Register   Sign In
Overriding $config['url_suffix'] in specific controllers/methods
#1

[eluser]Roger Glenn[/eluser]
PROBLEM
The majority of my URLs need a trailing slash so I set the url_suffix to '/' in my config file.

Code:
/*
|--------------------------------------------------------------------------
| URL suffix
|--------------------------------------------------------------------------
|
| This option allows you to add a suffix to all URLs generated by CodeIgniter.
| For more information please see the user guide:
|
| http://ellislab.com/codeigniter/user-guide/general/urls.html
*/

$config['url_suffix'] = '/';

But I need to remove the trailing slash on a few important URLs.

I am using CI to dynamically generate various sitemap.xml files through a sitemap.php controller, so I declared the following routes in my routes.php file:

Code:
// sitemaps
$route['sitemap-index.xml']     = "sitemap";
$route['main-sitemap.xml']      = "sitemap/main";
$route['locations-sitemap.xml'] = "sitemap/locations";
$route['artists-sitemap.xml']   = "sitemap/artists";
$route['venues-sitemap.xml']    = "sitemap/venues";

But because I've set the $config['url_suffix'] = '/' these .xml URLs getting a trailing slash, ex.

Code:
http://domain.com/sitemap-index.xml/

Whenever I remove the slash from my browser's address bar and hit enter I always get re-routed to the version with the trailing slash.

POSSIBLE SOLUTION
I've tried overriding the config item in the sitemap.php controller constructor like so

Code:
function __construct() {
parent::__construct();
$this->config->set_item('url_suffix', '');
}

...but that has no effect.

Is there a hidden framework method I can use to remove the $config['url_suffix'] from specific URL's in my application?
#2

[eluser]CroNiX[/eluser]
Why do you need to have a trailing slash on some urls? That generally indicates a directory.
#3

[eluser]Roger Glenn[/eluser]
Yes, you are correct. The client is an SEO company so I really can't question their instructions for URL structure.

They requested that all URLs within the application have a trailing slash, but I need to remove the trailing slash on these 5 system-generated sitemap.xml pages.

#4

[eluser]CroNiX[/eluser]
Strange, every SEO company I've done work for had the opposite requirements. They've always said that directories get less preference in rankings than actual pages, and the fewer segments there are generally the higher ranking it is as the content is deemed to be more important.

Well, not sure, but it might be best to do this via htaccess/rewrite, and have it ignore certain requests via a negative rule. Not the best solution, but could be pretty quick to do. The drawback would be if you add more urls that don't require the trailing slash you'd have to update the htaccess, which isn't very dynamic.

Something like (if its not sitemap-index.xml or main-sitemap.xml, add a trailing slash)
Code:
RewriteCond $1 !^(sitemap-index\.xml|main-sitemap\.xml)
RewriteRule ^(.+)/$  /$1 [R=301,L]
...then CI rules...

I'm sure you could extend CI to do what you want, but I haven't ever looked at the code for, or used, the url_suffix so can't really advise how to approach it that way. But that would be the preferable way, although it would probably take a lot longer to figure it out than the htaccess/rewrite.
#5

[eluser]CroNiX[/eluser]
Another solution might be to actually write your xml files out to the filesystem instead of having them generated in realtime by the controller. I do that via a nightly cron job (generate the xml) and then ping google to alert them if the sitemap changed. You can just save them in the root of your site and then you could get rid of the routes.

Then if you use "/" as the url_suffix, along with the following htaccess rule which says if its NOT a real file or directory, to process the request with CI, it might work.

Code:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
#6

[eluser]Roger Glenn[/eluser]
Thanks CroNIX for the insights and suggestions.

I may have given too much information in my original post. It really doesn't matter what the specific url_suffix is, I would just like the ability to override it as-needed within my application (without having to hack the core).

For now I have created static .xml files in the filesystem. I also realized I could do something like your second suggestion, writing these files out to the filesystem via cron (or possibly a button in their control panel).

Still though, it'd be great if we could do what I'm asking.
#7

[eluser]Aken[/eluser]
I would force slashes using .htaccess, not PHP. You can then check for a file extension on dynamic URLs as a catch-all, and add any very specific exceptions if necessary.

I don't know why the SEO company wouldn't just do that themselves. After all, that's the business they're supposed to be experts in...




Theme © iAndrew 2016 - Forum software by © MyBB