Rewrite PyroCMS blog URLS? - apache

Say I have example.com set up with a PyroCMS installation, everything is working fine.
I've created a few posts on the blog and categorised them, so I end up with URLs like the following:
example.com/blog/category/foo
example.com/blog/category/bar
I'd like to access those categories with much shorter URLs, like the following:
example.com/foo
example.com/bar
I've tried the following (and several variations on it) in the .htaccess but gotten nothing but 500 errors and infinite redirects. It's the standard file as per github that I'm working with, with my additions below:
<IfModule mod_php5.c>
RewriteRule ^(.*)$ index.php/$1 [L]
RewriteRule foo /blog/category/foo [L]
</IfModule>
If anyone knows how to do this it would be much appreciated :)

I'm sure this is possible and preferable at the .htaccess level but I've settled for a PHP based approach for now. Specifically, adding some routes to the top of my system/cms/config/routes.php:
$route['(foo)'] = 'blog/category/$1';
$route['(bar)'] = 'blog/category/$1';
I had a follow up question of how to get this to work with examples.com/ (i.e. the site root), this approach doesn't work for that though unfortunately.

Related

htaccess redirect without .php extension

I recently changed a directory /old_dir/ to be /new_dir/ using this:
RedirectMatch 301 /old_dir/(.*) /new_dir/$1
Which seems to be working perfect for the url:
http://www.mysite.com/old_dir/test.php?var=xxxx
goes to
http://www.mysite.com/new_dir/test.php?var=xxxx
where test.php is the filename. But in many places I use:
http://www.mysite.com/old_dir/test?var=xxxx
which comes up with:
The requested URL /old_dir/test was not found on this server.
not using the .php extension takes advantage of some sort of apache plugin that knows it's a php handler, which seemingly messes up redirect because it says it doesn't exist now.
I am not sure how to fix this issue.
Edit: All the solutions are for this special case, but note that i have about 1000 other files that may not be php, or named the same.
For right now I just made a symbolic link in the old_dir with the name "test" to point to the new_dir's test.php. But I am still looking for a non-specific solution that includes my scenario.
Have you ever tried using mod_rewrite?
Options +FollowSymlinks
RewriteEngine On
RewriteRule ^([^/]+)/([^/]+)$ $1/$2.php [QSA]
RewriteRule ^old_dir/([^/]+)/$ new_dir/$1.php [QSA]

Codeigniter and RewriteRule problem

I've been puzzling over this none for a while. This is the rewrite rule that I've got at the moment:
Options +FollowSymlinks
DirectoryIndex index.php
RewriteEngine on
RewriteRule ^ext\/(.*)$ index.php/cproposal/key/$1 [NC]
Essentially I'm trying to get
http://localhost/cvc/ext/12445345346
to rewrite to
http://localhost/cvc/index.php/cproposal/key/12445345346
Codeigniter is producing a 404 however. If I change the index.php/cproposal/key/$1 part of the rule to something inane like the Codeigniter license.txt thats found in the directory then it works but anything actually related to Codeigniter itself produces a 404.
Any ideas where I'm going wrong?
mod_rewrite is great, but Codeigniter already has a built in solution for anything in your Codeigniter application.
Try adding this to your config/routes.php:
$route['ext/(:num)'] = 'cproposal/key/$1';
This would route requests for:
http://localhost/cvc/ext/{any_number}
to
http://localhost/cvc/cproposal/key/{requested_number}
The index.php will be optional, and dependent on your CI configuration. In other words, if you're using it already - it will be in the url. If not, it doesn't have to be.
All CI routes may use regular expressions in addition to the built in wildcards (:num) and (:any), so feel free to get creative.

Using .htaccess mod_rewrite to pass all URLs in a given directory to a single redirect script

Im trying to use mod_rewrite to redirect any call to /real-estate/* to rewrite.php...i know i can redirect everything to rewrite.php with this:
RewriteRule ^(.*)$ rewrite.php?url=$1 [L]
I would like to have my urls formatted like /real-estate/12345/123-anywhere-st ....where the 123-anywhere-st would be ignored, and have /real-estate/12345 sent to rewrite.php...id like the rewrite rule to only be used on /real-estate...all other areas of the site should function as is...Ive searched all over for a good tutorial or cheat sheet, but none that I can find actually explain how to format the mod_rewrite rules, they just give one or two examples and thats it...can anyone help, as well as maybe provide a link to somewhere I can learn
Thanks!
RewriteRule ^/real-estate/(.*)$ rewrite.php?url=$1 [L]

mod_rewrite, only access one part of URL for blog entry

In other sites I've worked on, I have been able to easily generate search engine friendly URLS using the following in .htaccess:
<files entry>
ForceType application/x-httpd-php5
</files>
I'm working on a site hosted by a company that runs PHP as fast CGI and this does not work. I am trying to achieve the following URL - http://somewhere.com/blog/entry/12/this-is-the-title
I am just looking for the id (12 in the example) and do not necessarily need the title (my logic behind this being that the title might be changed by the client, links might be broken). I tried the following mod_rewrite but it does not work if I add the title:
RewriteEngine on
RewriteRule ^entry/([^/\.]+)/?$ index.php?id=$1 [L]
I've never worked with mod_rewrite before and a lot of the documentation I've come across is about achieving far more complex results. Any help would be appreciated
If the .htaccess file is in fact placed as /blog/.htaccess, you will need a RewriteBase /blog/ line.
Just looking for numbers will also help limit the returned id.
RewriteRule ^blog/entry/([01-9]+)/?$ index.php?id=$1 [L]
This also drops the '/?$' part of the regex. The '$' at the end anchors the match to the end of the string - you just need the numbers, and can ignore anything that is not a digit.

mod_rewrite to alias one file suffix type to another

I hope I can explain this clearly enough, but if not let me know and I'll try to clarify.
I'm currently developing a site using ColdFusion and have a mod_rewrite rule in place to make it look like the site is using PHP. Any requests for index.php get processed by index.cfm (the rule maps *.php to *.cfm).
This works great - so far, so good. The problem is that I want to return a 404 status code if index.cfm (or any ColdFusion page) is requested directly.
If I try to block access to *.cfm files using mod_rewrite it also returns a 404 for requests to *.php.
I figure I might have to change my Apache config rather than use .htaccess
You can use the S flag to skip the 404 rule, like this:
RewriteEngine on
# Do not separate these two rules so long as the first has S=1
RewriteRule (.*)\.php$ $1.cfm [S=1]
RewriteRule \.cfm$ - [R=404]
If you are also using the Alias option then you should also add the PT flag. See the mod_rewrite documentation for details.
Post the rules you already have as a starting point so people don't have to recreate it to help you.
I would suggest testing [L] on the rule that maps .php to .cfm files as the first thing to try.
You have to use two distinct groups of rewrite rules, one for .php, the other for .chm and make them mutually exclusives with RewriteCond %{REQUEST_FILENAME}. And make use of the flag [L] as suggested by jj33.
You can keep your rules in .htaccess.