Why won't this mod_rewrite rule work with CodeIgniter? - .htaccess

RewriteRule ^(new-york/new-york-city/chelsea/10001) index.php [L]
The above code does not work. It gives me a 404 error in Codeigniter. However, the following code does work.
RewriteRule ^(new-york/new-york-city/chelsea/10001) http://anywebsitehere.com/ [L]

The RewriteRule just rewrites to index.php, which calls the default route, if you have one setup. If you want to go to your search, you must rewrite to the search controller and method and pass the pretty URL or part of it as arguments
RewriteRule ^new-york/new-york-city/chelsea/10001 index.php/search_controller/search_method/$0 [L]
This will call in application/controllers/search_controller.php the method search_method with the pretty URL as arguments
search_method('new-york', 'new-york-city', 'chelsea', '10001');

Related

htaccess - rewrite rule - redirect after url rewrite

This is pretty simple, but can't seem to figure this out.
I have a url /shows/index.php?id=1 that I wanted to appear in the url as /shows/1/index.php.
So I added this to my htaccess:
RewriteRule ^shows/([0-9]*)/index.php$ /shows/index.php?id=$1 [L]
This worked like a charm. I can now navigate to /shows/1/index.php no problem at all. However, I can also still navigate to /shows/index.php?id=1. I wanted that page to automatically redirect to the new url, so I wrote this:
RewriteRule ^shows/index.php?id=([0-9]*)$ /shows/$1/index.php [R=301,L]
...but it doesn't do anything. However, if I do something like:
RewriteRule ^shows/([0-9]*)/0$ /shows/$1/index.php [R=301,L]
It redirects just fine. So that makes me think there is an issue with the first part of the rewrite rule, maybe? I'm not too familiar with this sort of stuff.
Since you want to redirect and rewrite the same request, you need to match against %{THE_REQUEST} variable otherwise you will get an infinite loop error on the redirect
RewriteEngine on
# redirect /shows/index.php?id=1 to /shows/1/index.php
RewriteCond %{THE_REQUEST} /shows/index.php\?id=([^\s]+) [NC]
RewriteRule ^shows/index.php$ /shows/%1/index.php? [NC,L,R]
#rewrite /shows/1/index.php to /shows/index.php?id=1
RewriteRule ^shows/([0-9]*)/index.php$ /shows/index.php?id=$1 [L]
Clear your browser cache before testing this.

.htaccess | no redirect if there is an specific parameter

im doing a cms at the moment
now im struggeling with the ajax implementation
i have everything running except a mod_rewrite problem..
RewriteEngine On
RewriteRule \.html index.php [L]
this redirects nothing except html files to index.php
i need a second rule witch checks the REQUEST_URI for a parameter to prevent the full site gets loaded by ajax.
i dont think this is understandable so i just post what i want to achieve^^
RewriteEngine On
RewriteCond %{REQUEST_URI} !(?rewrite=no$)
RewriteRule \.html index.php [L]
i want nothing redirected except html files and also no redirect on url's with "(.html)?rewrite=no" at the end
hope someone can help me since rewrites and regexp are not my stongest stuff
thanks in advance
From the Apache docs:
REQUEST_URI
The path component of the requested URI, such as "/index.html". This notably excludes the query string which is available as as its own variable named QUERY_STRING.
So you are actually looking to match on %{QUERY_STRING} rather than %{REQUEST_URI}. Don't include the ? on the query string when matching its condition:
RewriteEngine On
# Match the absence of rewrite=no in the query string
RewriteCond %{QUERY_STRING} !rewrite=no [NC]
# Then rewrite .html into index.php
RewriteRule \.html index.php [L]

Rewrite rules error

I want to rewrite a simple url, but without generating google errors
This code works :
RewriteEngine on
RewriteRule lieu/([0-9]+).* index.php?com=location&lID=$1 [L]
RewriteRule evenement/([0-9]+).* index.php?eID=$1 [L]
but i want to add R=301 flag for SEO
When i add [R=301,L] :
The requested URL /var/www/mysite/index.php was not found on this server.
I know that R=301 flag must be used with http://
but when i try the url is not rewritting
Apache tries to guess whether a path is a URI-path or a file-path, and it's guessing wrong. When you are internally rewriting, a file-path is perfectly fine, because it's all internal to the server. But when you are redirecting, apache incorrectly guesses that your target (the index.php?eID= ) is a file-path and it gets flagged to be handled by mod_alias as a redirect. By the time the redirect happens, it's malformed as a file-path instead of URI path. That's why you're getting the /var/www/mysite/ bit when you redirect.
Either add a RewriteBase to provide a URI base for relative URIs, or make your target an absolute URI:
RewriteEngine On
RewriteBase /
RewriteRule lieu/([0-9]+).* index.php?com=location&lID=$1 [R=301,L]
RewriteRule evenement/([0-9]+).* index.php?eID=$1 [L,R=301]
or
RewriteEngine on
RewriteRule lieu/([0-9]+).* /index.php?com=location&lID=$1 [L,R=301]
RewriteRule evenement/([0-9]+).* /index.php?eID=$1 [L,R=301]

mod_rewrite so that first-level subdirectory is a GET variable

Alright, title is REALLY sloppy.
Here's my problem: I have a news site and when you go to the main page (domain.com) it redirects you to domain.com/news/top?geography=San_Francisco after it figures out your geography.
How do I use the .htaccess so that it goes from domain.com/news/top?geography=San_Francisco domain.com/San_Francisco/news/top ?
There are some similar questions, but I have not found one similar enough in that you're editing the URL as a furtherback subdirectory.
It should also be noted that I am using the Code Igniter framework for PHP and it normally has it as domain.com/index.php/news/top?geography=San_Francisco but I did a mod_rewrite already to get rid of the index.php. The code is as follows for that:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
Code I've tried:
RewriteRule ^([^/]+)/news/top$ /news/top?geography=$1 [L,QSA]
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
Before the index.php rule that you have, try adding this:
RewriteRule ^([^/]+)/news/top$ /news/top?geography=$1 [L,QSA]
You'll need to make sure the links you generate are in the form of domain.com/San_Francisco/news/top though.
But to take care of the links in the wild that still look like the old way, you have to match against the actual request:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /news/top\?geography=([^&]+)
RewriteRule ^news/top$ /%1/news/top? [L,R=301]
This will 301 redirect the browser if someone goes to the link domain.com/news/top?geography=San_Francisco and make it so the browser's address bar says this: domain.com/San_Francisco/news/top. At which point the browser will send another request for the second URL, and you use the rule above to change it back into the one with a query string.

RewriteRule does not work, while the rest do

My blog's .htaccess is setup in such a way that one page is accessed through multiple URLs, and displays different content depending on which URL is visited.
http://kn3rdmeister.com/category/blog/
http://kn3rdmeister.com/2012/
http://kn3rdmeister.com/2012/07/
all are actually using http://kn3rdmeister.com/blog.php.
The .htaccess file is very handy in the sense that I only need to redirect to one page (pretty much ever) just with different query strings. After a lot messing around with 'em, all of my rules finally work, and I'm dang glad that they do. Well, almost all of them work. The last one does not.
the .htaccess:
RewriteEngine On
RewriteRule ^blog\.php$ /category/blog/ [R=301,L]
RewriteRule ^category/blog/?$ blog.php [L]
RewriteRule ^category/blog/page/?$ /category/blog/ [R=301,L]
RewriteRule ^category/blog/page/([0-9]*)/?$ /category/blog/?pagenum=$1 [L]
RewriteRule ^([0-9]{4})/?$ /category/blog/?year=$1 [L]
RewriteRule ^([0-9]{4})/([0-9]{2})/?$ /category/blog/?year=$1&month=$2 [L]
RewriteRule ^([0-9]{4})/([0-9]{2})/([0-9]{2})/?$ /category/blog/?year=$1&month=$2&day=$3 [L]
RewriteRule ^([0-9]{4})/([0-9]{2})/([0-9]{2})/(^/]+)/?$ /category/blog/?url=http://kn3rdmeister.com/$1/$2/$3/$4/ [L]
The last rule is supposed to redirect to the "permanent link" page for each blog post. Being that each URL is unique, I'm using the post URLs as the unique identifier. Essentially, it is supposed to pass the "url" query string through "blog.php". The PHP script takes over, sees that the "url" query string is set, and then loads the only post with that exact URL in it's row.
The script works, but the redirect doesn't. Going directly to
http://kn3rdmeister.com/blog.php?url=http://kn3rdmeister.com/2012/07/04/amsterdam-ave/
will load the right content. However, going to
http://kn3rdmeister.com/2012/07/04/amsterdam-ave/
doesn't.
Try adding QSA (Query String Append). Also, invert rules so that "deeper" links go on top.
RewriteRule ^([0-9]{4})/([0-9]{2})/([0-9]{2})/(^/]+)/?$ /category/blog/?url=http://kn3rdmeister.com/$1/$2/$3/$4/ [QSA,L]
RewriteRule ^([0-9]{4})/([0-9]{2})/([0-9]{2})/?$ /category/blog/?year=$1&month=$2&day=$3 [QSA,L]
RewriteRule ^([0-9]{4})/([0-9]{2})/?$ /category/blog/?year=$1&month=$2 [QSA,L]
RewriteRule ^([0-9]{4})/?$ /category/blog/?year=$1 [QSA,L]
But, you can't use rewritten links in other rules. So wherever you have category/blog/ replace it with blog.php.
Whilst webarto comments are good advice, your problem is a missing [:
^([0-9]{4})/([0-9]{2})/([0-9]{2})/([^/]+)/?$
not
^([0-9]{4})/([0-9]{2})/([0-9]{2})/(^/]+)/?$

Resources