I'm using apache with mod_rewrite and codeigniter to do my routing.
I've got two seperate (wildcard)domains pointing to my website, say: *.foo.com and *.bar.com
*.foo.com/somepath should to rewrite to *.foo.com/index.php/somepath
and *.bar.com.somepath should to rewrite to *.bar.com/index.php/bar/somepath
This is my .htaccess:
RewriteEngine on
RewriteCond %{HTTP_HOST} [^.]{3}\.bar\.com [NC]
RewriteCond $1 !(index\.php|assets|favicon|bar)
RewriteRule ^(.*)$ /index.php/pzdossier/$1 [L]
RewriteCond $1 !(index\.php|assets|favicon)
RewriteRule ^(.*)$ /index.php/$1 [L]
This gives me the correct result when I go to dev.foo.com/whatever
but when I go to dev.bar.com/whatever it just rewrites to dev.bar.com/index.php/whatever instead of pzdossier.
If I change
RewriteRule ^(.*)$ /index.php/pzdossier/$1 [L]
to
RewriteRule ^(.*)$ /index.php/pzdossier/$1 [L,R=302]
It works, I get redirected to /index.php/pzdossier/whatever.
What am I doing wrong? why does a redirect work, but a rewrite doesnt?
Codeigniter was using the $_SERVER['request_info'] to determine what controller to call.
When I changed that to $_SERVER['PATH_INFO'] it correctly using the .htaccess rules as I wanted.
So change application/config/config.php $config['uri_protocol'] to PATH_INFO
Related
I have a website, let's say www.example.com but this used to be www.example.nl. All traffic from www.example.nl is now redirected to www.example.com, but as we changed some naming conventions, www.example.nl/seeds now has to redirect to www.example.com/nl/flower-seeds.
The .htaccess file I got contains the following code:
RewriteEngine On
RewriteBase
RewriteCond %{HTTP_HOST} !example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L, R=301]
Redirect 301 /seeds/(.*) example.com/nl/flower-seeds/$1
When I navigate to www.example.nl/seeds/ I end up at www.example.com/seeds/ which ends up in a 404 because I'm missing the /nl/flower- part.
I think Redirect doesn't work properly when the URL is already altered by a RewriteRule. How would you tackle this problem? Any help is appreciated!
You should exclude /seeds/ directory form general rules like this :
RewriteEngine On
RewriteCond %{HTTP_HOST} !example.com$ [NC]
RewriteCond %{REQUEST_URI} !/seeds/(.*)
RewriteRule ^(.*)$ http://www.example.com/$1 [L, R=301]
RewriteCond %{HTTP_HOST} !example.com$ [NC]
RewriteRule ^seeds/(.*)$ http://www.example.com/nl/flower-seeds/$1 [L, R=301]
Note: clear browser cache then test
Also , don't use regex with redirect like what you did :
Redirect 301 /seeds/(.*) example.com/nl/flower-seeds/$1
Here this (.*) has no meaning , you could use either RedirectMatch or RewriteRule
I am trying to implement 301's for sub-pages on my site which is hosted on an Apache server (Fasthosts), using Rewrite scripts in the .htaccess file. I have tried to follow many pieces of documentation (infact, I've never had any problems implementing 301's using .htaccess before!) but on this particular website, nothing seems to work.
There is currently a 301 re-direct in there from non-www to www which is working fine. There are also some other snippets using regex which I imagine are for the CMS.
Below is the current state of the .htaccess file. An example of a 301 I'm trying to re-direct is in there (Lines 5 & 6)
Old page: http://www.junkwize.com/home-Garden%20Clearance
to New page: http://www.junkwize.com/services/garden-clearance-london
.htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^junkwize.com
RewriteRule (.*) http://www.junkwize.com/$1 [R=301]
RewriteCond %{HTTP_HOST} www.junkwize.com/home-Garden%20Clearance
RewriteRule (.*) http://www.junkwize.com/services/garden-clearance-london [R=301]
#Options +FollowSymlinks
RewriteRule ^.htaccess$ — [F]
RewriteRule ^([/admin]+)$ admin/login.php [L]
RewriteRule ^([/admin]+)([/blocks]+)$ admin/login.php [L]
RewriteRule ^([/blocks]+)$ index.php [L]
# RewriteRule ^([^/\.]+)-([^/\.]+)-([^/\.]+)$ index.php?main=$1&id=$2&menu=$3 [L]
RewriteRule ^([/deals]+)-([^/\.]+)$ index.php?main=$1&id=$2 [L]
# RewriteRule ^([^/\.]+)-([^/\.]+)$ index.php?main=$1&leftmain=$2 [L]
RewriteRule ^([^/\.]+)/([^/\.]+)$ index.php?main=$1&leftmain=$2 [L]
RewriteRule ^([^/\.]+)$ index.php?main=$1 [L]
Any help would be much appreciated.
Many thanks.
This code is faulty and is not going to work:
RewriteCond %{HTTP_HOST} www.junkwize.com/home-Garden%20Clearance
RewriteRule (.*) http://www.junkwize.com/services/garden-clearance-london [R=301]
Reason is that RewriteCond %{HTTP_HOST} can only match host name. Replace that code with this:
RewriteRule ^home-Garden\ Clearance/?$ /services/garden-clearance-london [R=301,L,NC]
I've set up wildcard domains locally for testing on .dev
I'm trying to rewrite the following URL:
http://location.domain.dev/
to
http://www.domain.dev/site/location
I would like any requests with www in the subdomain to always go to www.domain.dev but if any request is made to location.domain.dev, I would like to keep that request in the address bar (i.e i dont want people to see the underlying change)
I currently have the following in my .htaccess
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.dev
RewriteRule ^(.*)$ http://domain.dev/site/%1 [QSA,NC]
# Removes index.php
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]
</IfModule>
Is this even possible?
You're pretty close. In order to not redirect the browser (causing the address bar to change) you need to get rid of the http://domain.dev part of the rewrite rule's target:
RewriteRule ^(.*)$ /site/%1/$1 [QSA,NC]
assuming that both *.domain.dev and www.domain.dev have the same document root. If they're different, you may have to enable mod_proxy and add a P flag so that the request gets proxied instead of redirecting the browser:
RewriteRule ^(.*)$ http://domain.dev/site/%1/$1 [QSA,NC,P]
My client has a godaddy Virtual Dedicated Server (yes I know it sucks).... It has WHM VPS Optimized running on it .
I am trying to remove the index.php from the urls but my server still displays 404 errors I have looked at every forum in the universe and cant get an htaccess file that works. Here is what expression engine tells you to put use...
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} !^(/index\.php|/img|/js|/css|/robots\.txt|/favicon\.ico)
RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>
I know diddly about Apache.. please help kind sirs...
Test if the htaccess is working:
# Redirect domain
RewriteCond %{HTTP_HOST} ^www.yourdomain.com$
RewriteRule ^/?$ "http\:\/\/google\.com" [R=301,L]
Or
# Remove the www from the URL
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
Ensure you actually have content appearing when you enter /index.php :P
Once you know your htaccess is working try "$1" instead of "%{REQUEST_URI}":
RewriteEngine on
RewriteCond $1 !^(img|admin\.php|themes|robots\.txt|index\.php|sitemap\.xml|/favicon\.ico) [NC]
RewriteRule ^(.*)$ /index.php?/$1 [L]
Also try "/index.php?/$1" instead of "/index.php/$1"
Ensure you have set EE (Admin->General Configuration) so "Name of your site's index page" is blank.
Remember that once you do get EE to work without the index.php appearing, it can be used both ways (with and without), which is bad for SEO, so remove it like so (place before the above rewrite :)
# Redirect index.php Requests (prevent site showing index.php in browser address bar)
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,L]
i would like to shorten
www.site.com/product/info/laptop to www.site.com/laptop
I used
RewriteRule ^(.+)$ /product/info/$1
but i get 500 Internal Server Error
when i try,
RewriteRule ^([a-zA-Z0-9_\s\'~%,:!?()_=&-]+)$ /product/info/$1
it works but i want to support the period as well, so when I include .
RewriteRule ^([a-zA-Z0-9_\s\'~%,:!?()\._=&-]+)$ /product/info/$1
It gives me 500 Internal Server Error
Could you explain what is going on?
Thank you
Try the following:
RewriteCond %{REQUEST_URI} ^/product/info/(.*)$
RewriteRule ^(.*)?$ /%2 [R=301,L]
That will redirect the browser from www.example.com/product/info/laptop to www.example.com/laptop with a "Moved Permanently" header.
If you mean you wish the shorter URL to point to the longer URL internally, then you must avoid circular redirections:
RewriteRule ^product/info/.*$ - [L] # don't redirect again
RewriteRule ^(.*)$ /product/info/$1 [L] # redirect everything else
The first line will stop trying to redirect www.example.com/product/info/laptop to www.example.com/product/info/product/info/laptop, and so-on.
Edit
Based on your comment, it looks like you're also trying to redirect everything except img, phpmyadmin, etc, to index?whatever - You have to rearrange it all a bit now, something like this:
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/(index\.php|img|phpmyadmin|images|user|wmd|FCKeditor|map|jscalendar|aurigma|xajax_js|css|js|include|floatbox|helper|styles|ajax|robots\.txt|favicon\.ico|product/info/(.*))
RewriteRule ^(.*)$ - [L] # don't redirect these
RewriteRule ^(.*)$ /product/info/$1 # redirect everything else
I'm not 100% on the "product/info/(.*)" part of the first rewrite. If that doesn't work, try this:
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/(index\.php|img|phpmyadmin|images|user|wmd|FCKeditor|map|jscalendar|aurigma|xajax_js|css|js|include|floatbox|helper|styles|ajax|robots\.txt|favicon\.ico)
RewriteRule ^(.*)$ - [L] # don't redirect these
RewriteRule ^product/info/.*$ - [L] # don't redirect again
RewriteRule ^(.*)$ /product/info/$1 [L] # redirect everything else
Edit 2
Final answer based on your comment:
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/(index\.php|img|phpmyadmin|images|user|wmd|FCKeditor|map|jscalendar|aurigma|xajax_js|css|js|include|floatbox|helper|styles|ajax|robots\.txt|favicon\.ico)
RewriteRule ^(.*)$ - [L] # don't redirect these
# pass controllers to index.php
RewriteCond %{REQUEST_URI} ^/(home|browse|calendar|star|member|enter|product)
RewriteRule ^(.*)$ /index.php?$1 [L]
# pass other things than controllers to /index.php?/product/info/$1
RewriteRule ^(.*)$ /index.php?/product/info/$1 [L] # redirect everything else