Codeigniter and RewriteRule problem - apache

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.

Related

Rewrite PyroCMS blog URLS?

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.

Apache - mod_rewrite how to prefer files instead of directories (if both have same name)?

I have very similar problem like this one: Apache mod_rewrite - prefer files over directories with pretty URLs
However it is not same, and solutions mentioned in above link doesn't work for me.
My directory structure looks like this:
/pages/articles/january.php
/pages/articles.php
/pages/home.php
/articles/
/index.php
Now, I am including in index.php pages (depending on url).
For example, when user types address www.domain.com (or www.domain.com/home), index.php will include /pages/home.php
But if I enter this URL: www.domain.com/articles it will make link something like this: www.domain.com/articles/?page[]=articles (in other words index.php won't include /pages/articles.php file)
On the other hand, this works perfectly: www.domain.com/articles/january
This is my htaccess file:
RewriteEngine On
RewriteBase /
RewriteRule ^([a-zA-Z0-9+]*)/([a-zA-Z0-9+]*)$ %{DOCUMENT_ROOT}/index.php?page[]=$1&page[]=$2 [QSA,L]
RewriteRule ^([a-zA-Z0-9+]*)$ %{DOCUMENT_ROOT}/index.php?page[]=$1 [QSA,L]
I use array for page because I can have subpages (and subpages work fine!).
EDIT: I have found this however it doesn't solves my problem :(
Can someone tell me why it does this and how to fix it?
Or, how can I give priority to files instead directories?
EDIT 2: I solved it by removing "/articles/" directory, however I am still interested how to make it work through htaccess file rules.
Per the comments above, your original issue was worked around by removing the articles directory, but you still wanted to be able to deal with directories in that sort of situation.
You'd probably want to split into two sets of rules. Have an earlier rule that uses the -d flag in a RewriteCond to catch directories so that it could treat them differently as needed. Alternately, ignore directories in the first rule by negating the flag (!-d) then catch them in the later rule.

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]

How do I rewrite www.sitename.com/thing/thing.php?otherthing=something-like-this to www.sitename.com/something-like-this?

How do I rewrite
www.sitename.com/thing/thing.php?otherthing=something-like-this
to
www.sitename.com/something-like-this?
please help me with this as I can't seem to succeed. My host uses apache 2.2. Many thanks for your help!
Update
No I don't need that trailing ? However, I used the Rewrite rule you offered me and it still ain't working. I also added a RewriteEngine On before the rules.
I have Linux hosting, .htaccess and the code is obviously semantically correct, cause otherwise I would get the all so popular 500 internal server error. I placed the .htaccess file in the folder thing and in the root of the site, but it still won't work.
There should be an option to display it in directory format instead of the PHP ? format. If not, you could use the .htaccess mod_rewrite rule to make that display in the /folder/ way.
The way I do it is that I just upload my files and each page name is index.html and then I create folders, and put each index.html in the folder. Like this:
/guidelines/
In that folder is index.html, so instead of it being /guidelines.html it's /guidelines/
Looks better without .html
You need to use mod_rewrite:
RewriteCond %{QUERY_STRING} ^otherthing=(.*)$
RewriteRule ^thing/thing.php$ /%1? [L]
No idea if you meant to have that trailing ? at the end of the rewrite, I don't think that's possible. Note that the ? at the end of the RewriteRule is to get rid of the query string, otherwise, the rewritten URL will still have the ?otherthing=something-like-this at the end.

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.