I have in my htaccess file the following code
RewriteRule ^post/(.*)$ index.php?p=$1 [QSA,R]
this works almost fine but appends the querystring in the url after rewriting. i dont want this. if i delete the R-Flag, even if i delete both flags, it wont work.
i want to overwrite
/post/123 => index.php?p=123
/page/123 => index.php?page_id=123
/category/123 => index.php?cat=123
but show /post/123 etc in browser url
There is no sense of redirecting [R] if you don't want the url to be change.
Try this:
RewriteRule ^post/(.*)$ index.php?p=$1 [L,QSA]
RewriteRule ^page/(.*)$ index.php?page_id=$1 [L,QSA]
RewriteRule ^category/(.*)$ index.php?cat=$1 [L,QSA]
You should append [L] flag also which terminate processing further rules.
Related
On my CodeIgniter site, I would like to add a specific rewrite rule, so that this url
http://www.exemple.com/cache.manifest
would rewrite to
http://www.exemple.com/controller/manifest
(because Safari 7 seems to only accept .manifest files for ApplicationCache)
So I try to add this line to my htaccess
RewriteRule ^cache.manifest$ controller/manifest
I added it before the other rewrite rules :
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^cache.manifest$ controller/manifest
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>
But it returns a 404. If I change the line to
RewriteRule ^cache.manifest$ test.html
it works. So the first part on my rule is correct.
If I try to access directly to www.example.com/controller/manifest, it works to, so my url is correct.
I tried also
RewriteRule ^cache.manifest$ index.php/controller/manifest [L]
But it doesn't work either…
Any clue ?
Thanks a lot
I tried some tests on my local server and I think the following might work:
RewriteRule ^cache.manifest$ /index.php/controller/manifest [R,L]
I am not entirely sure if you need the leading "/" or "/index.php", so you
may need to experiment.
You need the [R] flag to force a redirect. In this situation, you want Apache
to look for the string cache.manifest in the URL, and then go to the CI page
controller/manifest.
It appears that you need to explicitly set the redirect.
Please let me know if this works. Good luck!
I'm redeveloping a website using the codeigniter framework.
When we go live, we want to ensure a few of the old URLs will be redirected to the appropriate pages on the new site.
So I put what I thought would be the correct rules into the existing htaccess file, above the other rules that CodeIgniter applies.
However, they are not taking affect. Can anyone suggest what I'm missing here?
# pickup links pointing to the old site structure
RewriteRule ^(faq|contact)\.php$ /info/ [R=301]
RewriteRule ^registration\.php$ /register/ [R=301]
RewriteRule ^update_details\.php$ /change/ [R=301]
# Removes access to the system folder by users.
RewriteCond %{REQUEST_URI} ^_system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
# This snippet prevents user access to the application folder
# Rename 'application' to your applications folder name.
RewriteCond %{REQUEST_URI} ^myapp.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
# This snippet re-routes everything through index.php, unless
# it's being sent to resources
RewriteCond $1 !^(index\.php|resources)
RewriteRule ^(.*)$ index.php/$1
Try adding a [L]ast flag to your R=301 flag => [L,R=301] that makes sure no other rules are applied, and, just to be sure, try to redirect to a complete URL and, to be even more sure you haven't deleted anything, add RewriteEngine On to the top and set the RewriteBase.
Make your first rows look like
RewriteEngine On
RewriteBase /
RewriteRule ^(faq|contact)\.php$ http://www.YOURDOMAIN.XYZ/info/ [L,R=301]
and check if the URL in your browser changes when you call for instance the faq page.
I am attempting to rewrite any URL with "_excaped_fragment_=/some/directory" to "/some/directory?ajax=1". The code below is working correctly but i would like to do it without the redirect. This is a Wordpress site.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
AddDefaultCharset UTF-8
RewriteCond %{QUERY_STRING} ^_escaped_fragment_=(.*)$
RewriteRule (.*) %1?ajax=1 [L,R=301]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
If I remove "R=301" and just leave the "L" it does not work. I have read about [L] not stopping when from .htaccess, but i actually do not want it to stop. I would like it to just change the URL then go through the regular Wordpress rewrite.
Any ideas? Let me know if you need more information.
Edit:
Wanted to add an answerable question. Why does a redirect work and no redirect not work? Does the URL get changed as it works its way down the file or only on the last rule?
The [L] will stop any further processing in .htaccess if the rule matched.
The way you have this written the [L] will do that, but then you add [R=301] which is a redirect. That will cause the request to be sent back to the "top of the stack" in Apache for processing as if it was a completely new request, since you are doing a redirect. HOWEVER, now on the 2nd time through you've got the modified URL because the rewrite was processed before doing the hand-off.
In other words [L] does the mangling, then says "OK, we are done here, drop out to normal web processing now".
[L,R=301] does the mangling, then says "we are done with THAT, now got tell whomever is next is line that we've done a redirect".
That last part of the [L,R=301] triggers a whole new set of logic to process the "hey, we just got a redirect" rules. In your case, causing the entire rewrite sequence to be processed from the top with the mangled URL this time around.
I am having difficulty with code igniter routing.
http://www.mysite.com goes to the right controller and does the right thing. However, http://www.mysite.com/?ref=p&t2=455 causes a 404 error. Also http://www.mysite.com/mycontroller/mymethod/?ref=p&t2=455 works fine.
I changed the uri_protocol in the config.php file and tried different values. Auto seems to work the best.
My theory is that code igniter is using the query parameters to do the routing. The problem is that these query parameter have nothing to do with routing.
How do I tell code igniter to ignore query parameters for the default controller?
Note, I followed the instructions online to remove the index.php from the URL. I dont think its causing a problem, but here is my .htaccess file just in case:
RewriteEngine On
RewriteBase /~trifecta/prod/
#Removes access to the system folder by users.
#Additionally this will allow you to create a System.php controller,
#previously this would not have been possible.
#'system' can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn't true it sends the
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#This last condition enables access to the images and css folders, and the robots.txt file
#Submitted by Michael Radlmaier (mradlmaier)
RewriteCond $1 !^(index\.php|images|robots\.txt|css)
RewriteRule ^(.*)$ index.php?/$1 [L]
With that rewrite rule RewriteRule ^(.*)$ /index.php?/$1, here are some examples of rewrites that are occuring:
http://www.mysite.com =>
http://www.mysite.com/index.php?/ (actually, this might not be being rewritten at all)
http://www.mysite.com/mycontroller/mymethod/?ref=p&t2=455 =>
http://www.mysite.com/index.php?/mycontroller/mymethod/?ref=p&t2=455
http://www.mysite.com/?ref=p&ts=455 =>
http://www.mysite.com/index.php?/?ref=p&t2=455
The first one will work whether it is being rewritten or not. CodeIgniter is processing either an empty query string (which is easy) or a query string of simply "/".
The second one (which also works) is being rewritten, but CodeIgniter is able to process its query string, which is /mycontroller/mymethod/?ref=p&t2=455. CI turns that into an array of segments as
[0] => mycontroller
[1] => mymethod
[2] => ?ref=p&t2=455
Array index 2 ultimately gets ignored by anything you're doing.
The third one (which does not work is being rewritten, and CodeIgniter can't process its query string at all. Its query string is rewritten to: /?ref=p&t2=455. That makes for an array of segments that looks like this:
[0] => ?ref=p&t2=455
which doesn't match any controller on your site.
Probably, you'll fix the whole thing by modifying the RewriteRule from
RewriteRule ^(.*)$ /index.php?/$1 to
RewriteRule ^(.*)$ /index.php/$1
at which point you'd probably want to change the uri_protocol config back to PATH_INFO.
It seems to be a limitation in the way CodeIgniter is designed, not as a result of your routing and/or .htaccess. Someone filed a bug report here. However, instead of using this and adding your mymethod code into your index method of your default controller, you could use the _remap() function like so:
function _remap($method)
{
if ($method == 'index' && count($_GET) > 0)
{
$this->mymethod();
}
else
{
$this->index();
}
}
With the following configuration it works fine for me. http://www.site.com/?x=y gets routed to the index method of the default controller.
.htaccess
RewriteEngine on
RewriteCond $1 !^(index\.php)
RewriteRule ^(.*)$ index.php/$1 [L]
system/application/config/config.php
$config['base_url'] = "http://www.site.com/";
$config['index_page'] = "";
$config['uri_protocol'] = "PATH_INFO";
$config['permitted_uri_chars'] = 'a-z 0-9~%.:_\-?';
Remember to add ? to the list of permitted characters in the URL. Perhaps this is causing a problem. I just tried this setup with a clean installation of CodeIgniter and it worked fine. These were the only changes I needed to do.
Here is how I got around it:
1. I have a standard rewrite in .htaccess:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [QSA,L]
This is likely unrelated but...
in index.php the file outside the application folder I added:
$pattern = "/\?.*$/";
$replacement = '';
$_SERVER['REQUEST_URI'] =preg_replace($pattern, $replacement, $_SERVER['REQUEST_URI']);
this stops codeigniter from trying to use the querystring in the request. Now did you actually need the $_GET variables? They need to be parsed out of $_SERVER['REDIRECT_QUERY_STRING'] which will be set if you used mod_rewrite as above.
Try the CodeIgniter Super .htaccess file
I'm trying to rewrite the following url:
index.php?route=checkout/cart
to
/cart
using:
RewriteRule ^index.php?route=checkout/cart$ /basket [L]
However it doesn't seem to work. Anyone know what I'm doing wrong?
Thanks
RewriteRule does only test the URL path. You need RewriteCond to test the query:
RewriteCond %{QUERY_STRING} ^route=checkout/cart$
RewriteRule ^index\.php$ /basket [L,R=301]
The additional R=301 flag will cause an external redirect with the status code 301 (permanent redirect) instead of an internal redirect.
And if you want the other way round:
RewriteRule ^basket$ index.php?route=checkout/cart [L]
You need to send a redirect so that the new URL get reflected in browser address bar. So, add R to the [L].
RewriteRule ^index.php?route=checkout/cart$ /basket [R,L]
If you'd like that searchbots should ignore the "ugly" URL and/or remove it from the indexes and use the new instead, then send a 301 redirect.
RewriteRule ^index.php?route=checkout/cart$ /basket [R=301,L]