This question already has an answer here:
Closed 12 years ago.
Possible Duplicate:
How to rewrite to mobile.myUrl.com if user is mobile? (use Apache or inside Webapp?)
Hi there! I'm trying to write a rewrite rule, that would redirect every address in my domain, to index.php, like this:
RewriteCond %{REQUEST_URI} !=/index.php
RewriteRule .* /index.php
The problem with this code is that in index.php I check whether user came to my website from a pc, or a mobile device, and according to that I redirect him to appropriate version (index.html, or mobi.html) - now since this rule applies I'm stuck in a redirection loop. Pls help stackoverflowers :).
Either you just exclude the pages index.html and mobi.html:
RewriteCond %{REQUEST_URI} !=/index.php
RewriteCond %{REQUEST_URI} !=/index.html
RewriteCond %{REQUEST_URI} !=/mobi.html
RewriteRule .* /index.php
or you can set a GET variable in your redirected url, like header('Location: domain.com/index.html?norewrite'); and then do not rewrite the URL if it contains this variable:
RewriteCond %{REQUEST_URI} !=/index.php
RewriteCond %{QUERY_STRING} !^(.*&)?norewrite(&.*)?$
RewriteRule .* /index.php
Related
I am in the process of completely rewriting my old site in PHP and need to redirect the old pages to the new.
The old site made heavy use of perl scripts; in particular /cgi-bin/browse.cgi and /cgi-bin/search.cgi . I need these to redirect to /browse.php and /search.php respectively.
All other pages on the old site were fixed html pages in a hierarchical structure e.g. domain/place1/place2/place3.html . I need these to redirect to /index.php where the request is processed and PHP redirects implemented.
Currently I have this code in the .htaccess file:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
#RewriteRule ^cgi-bin/browse\.cgi(.*)$ ./browse.php?$1 [L]
#RewriteRule ^cgi-bin/search\.cgi(.*)$ ./search.php?$1 [L]
RewriteCond %{REQUEST_URI} !^search\.php
RewriteCond %{REQUEST_URI} !^browse\.php
RewriteCond %{REQUEST_URI} !^index\.php
RewriteRule ^(.*)$ ./index.php?%1 [L]
This does not currently cater for requests to domain/cgi-bin/search.cgi or similar browse.cgi, and every time I remove the # for the RewriteRules I get a 500 scripting error.
Basically, I need cgi-bin/browse.cgi requests to go to browse.php; similar for search.cgi, and everything else to go to index.php
Any help will be greatly appreciated.
Sigh...
I tried constructing some rewrite rules based on the following Stack Overflow questions:
Question 1
Question 2
My .htaccess currently looks like this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/(en|fr|es|de)/
RewriteRule ^(.*)$ /en/$1 [R,L]
RewriteRule ^(en|fr|es|de)/(.*)$ $2?locale=$1 [L]
and I've tried all sorts of variations (including the exact answers given in the questions).
As it stands my URL is redirecting to:
/en/?locale=en
When I look at the root with what looks like an infinite redirect loop in Firefox ('The page isn't redirecting properly').
As you've probably guessed I want it to rewrite to /en/ on the root and for it to look like that in the browser. But I want the real url to be /?locale=en. There is an index.php there on the root. Maybe I need another rule to take that into account?
I don't know, I'm really tired and exasperated and .htaccess has always been my downfall. Any insight appreciated.
You need to make sure there's no locale query string in your first rule:
RewriteCond %{QUERY_STRING} !locale=
RewriteCond %{REQUEST_URI} !^/(en|fr|es|de)/
RewriteRule ^(.*)$ /en/$1 [R,L]
Or you can try:
RewriteCond %{THE_REQUEST} \ /+(?!(en|fr|es|de)/).*
RewriteRule ^(.*)$ /en/$1 [R,L]
I have a controller named (for example) TheLink, and I can normally access it through mywebsite/index.php/TheLink. Is it possible to make a RewriteRule in htaccess to access it via mywebsite/index.php/The-Link without it redirecting to TheLink? So basically what I want it the URL to say mywebsite/index.php/The-Link (with dash) but use the TheLink controller.
I've tried
RewriteRule ^The-Link$ index.php/TheLink
[L]
but that just redirects me to the default CodeIgniter 404 Page
# To remove index.php from URL
RewriteEngine On
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
Additionally if website is hosted in another directory not just root, add this:
RewriteBase /some_directory/
Well, apparently this problem has nothing to do with htaccess and is solved in a completely different way. Solution found here:
Codeigniter Routes regex - using dashes in controller/method names
Edit: Also, a number of different solutions for this, including the one above, found here:
http://ellislab.com/codeigniter/forums/viewthread/124396/
Note: I had to add spaces because it thought I was posting links...
I have two sites coming into one server and one folder (foo.com and bar.com).
Foo.com needs to point at a page named foo.htm under the root of the site.
It also has the requirement of not changing the URL.
If the url is bar.com it needs to be left alone.
If the full url is http://www.foo.com/ it needs to be switched to the equivalent of
http://bar.com/foo.htm
Does that make sense?
I have the following which works for every page except the root page, which isn't redirecting to foo.htm.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^foo\.com
RewriteRule ^(.*)$ http://www.bar.com/$1
RewriteRule ^$ /foo.htm [L]
RewriteEngine On
RewriteCond %{HTTP_HOST} ^foo\.com
RewriteCond %{REQUEST_URI} ^/?$
RewriteRule ^$ http://www.bar.com/foo.htm [L]
RewriteCond %{HTTP_HOST} ^foo\.com
RewriteCond %{REQUEST_URI} ^/.
RewriteRule ^(.*)$ http://www.bar.com/$1
I'm not sure if it will run exactly like this as I can't test it - but if it doesn't work, just modify the regex in RewriteCond %{REQUEST_URI} .... possibly remove the slash and question mark... just try it.
This is my current .htaccess file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ $1/
RewriteRule ^([^/]+)/$ index.php?p1=$1 [L]
RewriteRule ^([^/]+)/([^/]+)/$ index.php?p1=$1&p2=$2 [L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/$ index.php?p1=$1&p2=$2&p3=$3 [L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/$ index.php?p1=$1&p2=$2&p3=$3&p4=$4 [L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/$ index.php?p1=$1&p2=$2&p3=$3&p4=$4&p5=$5 [L]
Basically, it takes up to five "Friendly url folders" and assign the value to varibles and then, send those to my index.php page
IE: http://www.example.com/ford/focus/grey/
$p1 = 'ford';
$p2 = 'focus';
$p3 = 'grey';
$p3 = 'grey';
So far, so good.
Now, I need to integrate a 301 instruction (RegExp?) in that same .htaccess because initially, I had GET parameters like this :
IE: http://www.example.com/ford/focus/grey/?lang=fr
I need to get rid of all GET variables because Google sees it as duplicate content (even if I'm using the nofollow attribute on my languages links)
IE: http://i.want.to.keep/my/url/?AND_DUMP_GET_VARIABLES
http://www.example.com/ford/focus/grey/?lang=fr
http://www.example.com/ford/focus/grey/?lang=en
http://www.example.com/ford/focus/grey/?lang=sp
==> http://www.example.com/ford/focus/grey/
Logically, the instruction should be interpreted between the first and the second block but I just don't know where to start. Any hints?
THANKS!
As I understand you want to get rid of the QUERY STRING and redirect (301 Permanent Redirect) to the same URL but without QUERY STRING.
The rule below will redirect EVERY request that has query string:
RewriteCond %{QUERY_STRING} !^$
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1? [R=301,L]
1. The ? will do the magic -- will strip query string
2. You desperately need this line: RewriteCond %{ENV:REDIRECT_STATUS} ^$. The problem is that it may not work on your Apache setup and I cannot give you what exactly you need to make it work (it works fine on my vanilla Apache v2.2.17 on Windows).
After rewrite (internal redirect) occurred, it goes to next iteration and Apache starts matching all rules from the top again but for already rewritten URL. If we not add the above line, then mod_rewrite will apply the above rule to rewritten URL form and you will end up with all URLs get rewritten to /index.php with no parameters at all.
If the above will not work, then try the code below:
# do not do anything for already existing files and folders
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .+ - [L]
RewriteCond %{QUERY_STRING} !^$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1? [R=301,L]
With help of # do not do anything for already existing files and folders rule, mod_rewrite will stop rewriting after URL will be rewritten to /index.php?p1=... format.
In case the above will not work at all (better -- in addition to the above -- I would suggest adding this anyway) use <link rel="canonical" href="FULL_PROPER_RUL"/> in your page:
http://googlewebmastercentral.blogspot.com/2009/02/specify-your-canonical.html
http://www.google.com/support/webmasters/bin/answer.py?answer=139394