URL rewriting whilst using codeigniter - .htaccess

I am using the following to remove my index.php in codeigniter -
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
I am replacing an old site with my new one written with codeigniter, because of this I need to rewrite some old urls to new ones. This is working fine for single pages, however I am running into a problem with the following rewrite -
RedirectMatch 301 ^/comments/(.*)/$ /location/$1
This should in theory redirect you from:
http://www.mysite.com/comments/123 to http://www.mysite.com/location/123
Because I’m removing index.php through a rewrite I am ending up being directed to -
http://www.mysite.com/location/123?/comments/123/
Does anyone know how to fix this?
Thanks.

You should not mix mod_alias (RedirectMatch) and mod_rewrite. Try this mod_rewrite rule instead:
RewriteRule ^comments/(.*)/$ /location/$1 [L,R=301]
Now just make sure to put this rule in front of your other rule.

Finally got it, for those that are interested, I had to add comments to a rewrite condition that I failed to mention in the first post:
RewriteCond $1 !^(index\.php|media|images|css|js|comments|robots\.txt)
Hopefully this may help somebody else.

Related

PHP Dynamic URL Parameters and GET

I'd like to ask if it's possible to use a dynamic url and GET at the same time.
Let's say my current dynamic url is: https://yourdomain.com/blog/this-is-a-title
Would it be possible to make this work too: https://yourdomain.com/blog/this-is-a-title?action=delete
RewriteEngine on
RewriteRule ^([0-9]+)$ index.php?id=$1
The dynamic url mentioned first works fine, but I want to make the second work as well.
This is my .htaccess - hope it helps.
PS: I know that the regex in my htaccess isn't correct, it's just an example.
Have your .htaccess Rules file in following manner. Please make sure that your htaccess Rules file is present in root folder(where blog and htaccess both are residing in it; htaccess shiouldn't be inside blog folder; should be place same folder with it). Make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
##Newly added rules here...
RewriteBase /blog/
RewriteCond %{THE_REQUEST} \s/blog/(?:[^?]*)?action=(\S+)\s [NC]
RewriteRule ^ index.php?action=%1 [L]
##Old Rules OP's htaccess ones.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9]+)$ index.php?id=$1

specific redirect required using mod_rewrite

I have to do a very specific url redirect using mod_rewrite within an .htaccess. Below is a url which has to map to the url below it:
m.example.com/123456/123456-product-name/
This needs to map to the following:
m.example.com/product-name/123456
I'm still getting to grips with regex and url rewrites and I've spent a good couple of hours trying to get this right. Can anybody help!?
Thanks in advance
You can do that. in root .htaccess:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^\d+/(\d+)-([^/]+) /$2/$1 [R=301,L]
Or just [L] (and not [R=301,L]), if you do that without redirection.

Straight Rewrite Rule for htaccess

I was looking for a simple .htaccess configuration that just convert /some_uri to /some_uri.php. Most examples in community are more complicated than I require. I was trying the following, but it didn't work:
Options +SymLinksIfOwnerMatch
RewriteEngine On
RewriteRule ^(.*)$ ./$1.php
Please help. Thank you.
You need to make sure that the rule does not match itself. In other words, you need to make sure the rule does not match if the url already ends on php. Besides that you probably want to prevent that the rule matches if it already points to a file that exists, so that you can normally serve css/js/images without it trying to append php to it.
RewriteCond %{REQUEST_URI} !\.php$ #Does not end with .php
RewriteCond %{REQUEST_FILENAME} !-f #Requested url is not an existing file
RewriteRule ^ %{REQUEST_URI}.php [L]

My htaccess isn't working for a redirect permanent

I'm trying to do a permanent redirect with .htaccess, but it isn't working and I have no idea why.
RedirectPermanent / http://www.flunchinvite.fr
I'm trying to do a redirection from : http://www.flunchinvite.com to: http://www.flunchinvite.fr.
Do you have any ideas?
Thanks
edit
I've just did a test to do a redirect to google, and it doesn't work either, whereas when I try to do a redirect with the same code on http://flunchinvite.fr it works. Do you know where that can come from ?
Try something similar to
//Rewrite to www
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^flunchinvite.com[nc]
RewriteRule ^(.*)$ http://www.flunchinvite.cfr/$1 [r=301,nc]
Use Rewrite if it is an option:
http://www.gnc-web-creations.com/301-redirect.htm
Another method we can use is via mod_rewrite. This requires that the
mod_rewrite module is active on your webserver. It usually is and is
done by the system administrators when they installed the webserver.
mod_rewrite is a very powerful URL re-writing engine and we will only
by scratching a hair on its head here.
Again, in your .htaccess file
RewriteEngine ON RewriteRule ^(.*)$ http://mynewdomain.com/$1
[R=301,L]
The above example will re-map your old domain to a new one and issue a
301 status code (permanent redirect). So a request for
http://olddomain.com/foobar.html will go to
http://mynewdomain.com/foobar.html
If you simply want to redirect all requests regardless of the page
requested to the new domain you could use:
RewriteRule /.* http://mynewdomain.com/ [R=301,L]
In this case no matter what file or directory is requested they will
all go to
http://mynewdomain.com/ i.e., http://myolddomain.com/foobar.html
will go to http://mynewdomain.com/
The [R=301,L] means redirect the client and send a 301 status code
(R=301) and make this the last rule (L).
At the end I did a php redirection, I don't know why it's not ok on the htaccess. I'll see that another time. I'm going to bed
Take a look at lines 5 and 6:
AddDefaultCharset UTF-8
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /demo2
RewriteCond %{HTTP_HOST} ^mathpdq\.com
RewriteRule ^(.*)$ http://www.mathpdq.com/demo2/$1 [R=permanent,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
I could not get 301 redirects to work so I went with this. basically if the user goes in with mathpdq.com/demo2 it forces a redirect to www.mathpdq.com/demo2.
The stuff below line 6 is just the normal mapping into the php functions.
http://pastie.org/5364605

Htaccess single page redirect problems

I'm having some issues setting up single page redirects using htaccess. Currently I have a htaccess file with:
RewriteCond %{SCRIPT_FILENAME} !-s
RewriteRule (.*) index.php?path=$1 [QSA,L]
Then a set of redirects e.g.
Redirect 301 /oldpage.htm http://www.mydomain.com/new-page
But the problem I am having is that when I go into a browser and type in the old URL, I get redirected to URL with a parameter attached, e.g:
"http://www.mydomain.com/new-page?path=oldpage.htm"
For some of my redirects this seems to work anyway, for some it produces a 404 error. I've also tried using RewriteCond and Rewrite Rule to write more generic catch all redirects for those pages that I can, and I'm having the same issue.
I'm thinking that some other rule must be interfering with my redirects - the only one I can see which might do so is the rule above, but if that was the case shouldn't the URL I am redirected to end up being
"http://www.mydomain.com/index.php?path=oldpage.htm"
Can anyone explain why parameters are being appended to the URLs and how I can stop this happening so that my redirects work?
Cheers!
This is because mod_alias (the Redirect directive) and mod_rewrite (the Rewrite* directives) are both being applied to the same URI in the URL-file mapping processing pipeline. In order to keep this from happening, you need to stick with one or the other in this case.
You also want the redirect to get applied first:
RewriteRule ^/?oldpage.htm$ http://www.mydomain.com/new-page [L,R=301]
RewriteCond %{SCRIPT_FILENAME} !-s
RewriteRule (.*) index.php?path=$1 [QSA,L]
Your index.php rule is going to catch all requests that aren't CGI scripts that are symlinks, so, pretty much everything if that's really your intention. Otherwise you can let legit requests get by unscathed by including:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
Above the last rule.
because your $1 in rewrite rule
RewriteRule (.*) index.php [QSA,L] try this

Resources