I have a rewrite rule to hide index.php, which is
working fine.
RewriteCond $1 !^(index\.php)
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
I am currently redirecting a specific sub-domain to another domain.
RewriteCond %{HTTP_HOST} ^(www\.)?deutschland\.example\.com$ [NC]
RewriteRule ^ http://www.example.de%{REQUEST_URI} [NE,R=301,L]
The redirect is working fine, but now I am getting index.php in the URL too, which is coming in the REQUEST_URI.
http://www.example.de/index.php/search/result
So how to remove 'index.php' from this redirected URL?
Note: Its the same php website application only, just using country-wise multiple domains.
(1) Rule order is important. (2) the last flag doesn't mean last; it means last on this cycle. (From Apache 2.4 the end flag does what you might think last does. See my Tips for debugging .htaccess rewrite rules for more discussion of this). So in this case rule(1) fires and then mod_rewrite loops around again and this time rule (2) fires giving what you find.
Swap the two rules around and it will work as expected.
Related
We have about 50 URLs that need to be redirected, but subdomain names may differ, for example:
http://abc.OLDdomain.com > http://cba.NEWdomain.com
http://foo.OLDdomain.com > http://bar.NEWdomainc.com
Most of the answers suggest rules, but in our case source / target subdomains often differ, so it's not a straightforward redirect rule that could solve it.
Also, can you do this type of redirect without rewrite rules?
So ideally something like
redirect 301 abc.OLDdomain.com http://cba.NEWdomain.com
Which does not currently work
I'm asking if there's a way to do it without rewrite rules because the htaccess is managed by someone who's not tech savvy but understands simple redirect directives such as the one above.
My current working solution with rewriterule/rewritecond is:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^abc.OLDdomain.com$ [NC]
RewriteRule ^(.*)$ http://cba.NEWdomain.com%{REQUEST_URI} [R=302,NC,L,QSA]
RewriteCond %{HTTP_HOST} ^foo.OLDdomain.com$ [NC]
RewriteRule ^(.*)$ http://bar.NEWdomainc.com%{REQUEST_URI} [R=302,NC,L,QSA]
If there's a one liner alternative without "RewriteCond and RewriteRule" that would be great
I have aim to use two domains (old and new). So when I go to address:
http://old.cz/whatever/whatever
I would like to get to:
http://new.cz/whatever/whatever
Perfectly works for me this thing:
RewriteRule ^(.*)$ http://www.new.cz/$1 [R=301]
But! At the same time I want following. When I go to address:
http://old.cz/
I want to get to:
http://new.cz/specific-page/specific-page
For that works this code:
Redirect 301 / http://new.cz/specific-page/specific-page
My issue is that in case I use both rules at the same time the first one is always prioritize and the second one suppressed. It means that when I go to http://old.cz/ I always get on only to http://new.cz/
Help me please.
Redirect and RewriteRule are directives of two different apache modules mod-alias and mod-rewrite . You can not combine these two directive for url redirection because of their different runtime behaviour. Use RewriteRule instead of Redirect.
RewriteEngine on
RewriteRule ^/?$ http://new.cz/specific-page/specific-page [L,R]
RewriteRule ^(.*)$ http://www.new.cz/$1 [R=301]
How would I redirect from the root folder to a sub folder and then mask that folder?
So instead of http://root.com/sub_folder
It would be just http://root.com
I have tried:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^root\.com$
RewriteRule (.*) http://root.com/$1 [R=301,L]
RewriteRule ^$ /sub [L]
However, that does not work. Any help will be welcome.
To clarify what I think you're looking for:
You want users who enter http://root.com with no trailing path to be rewritten silently to http://root.com/sub.
If a user directly enters http://root.com/sub, however, you want them to be redirected to http://root.com.
Any other path within root.com should be left alone.
The following two rules accomplish this. If you have more than one domain and only want this to apply to one domain, add your original RewriteCond in front of each RewriteRule.
RewriteRule ^sub/?$ http://root.com/ [R=301,L]
RewriteRule ^$ /sub [END]
First rule redirects /sub with or without trailing slash to root.com. Second rule rewrites base domain to /sub.
EDIT: Per Jon Lin's comment, below, the [L] flag only stops the current round of processing and internal rewrites are sent through the rules once more (I always forge that part). So, you can terminate the second line with [END] instead, which stops all rewrite processing. The catch is that [END] is only available in Apache 2.4 or higher, so if you're on an older version something trickier will need to be done.
On my site www.sqcp.com in testing on another linux server, all worked as it should. However since moving it to godaddy, the mod_rewrites haven't been working, therefore none of the other pages have been accessible. Even if I create a blank directory/folder in the what it's trying to tidy the url to it then works for that page (obviously isn't a fix).
So any help would be great here my .htaccess file.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ / [L,R=301]
RewriteRule (.*)/{2,}$ /$1/ [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} ^GET\ /[^?\s]+\.php
RewriteRule (.*)\.php$ /$1/ [L,R=301]
RewriteRule ^(.*)/(\d{4}\-\d{2}\-\d{2}\-[a-zA-Z0-9\-_]+)$ $1?s=$2 [L]
RewriteRule ^(.*/)?staff.php/([a-zA-Z0-9\-_]+) $1staff.php?s=$2 [L]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule (.*)/$ $1.php [L]
RewriteCond %{REQUEST_FILENAME}/index.php !-f
RewriteRule (.*)/$ $1 [L]
</IfModule>
Godaddy run a perfectly good shared hosting service, addressing a large market sector -- users who want an active site (that is with some scripting) but without the cost or complexity of paying for or having the expertise to administer their own Linux VM. This isn't a GoDaddy problem. Its yours.
So first get to understand the environment that you are running under by running a phpinfo script, and make sure it creates the variables that you use. As far as I can see on your example:
Rules 1-3 are 301 redirections to enforce some request naming convention.
Rule 1 redirects /index.php to /
Rule 2 collapses trailing multiple / to a single /
Rule 3 rewrites GET requests for *.php to *.php/
Rules 4-6 map public URIs to internal ones
Rule 4 rewrites /*/yyyy-mm-dd-word to *?s=yyyy-mm-dd-word (note no QSA)
Rule 5 seems to be attempting to rewrite /*/staff.php/word to /*/staff.php?s=word but the syntax is wrong for this.
Rule 6 replaces any trailing / by .php on redirection
Rule 7 strips any trailing '/' unless the uri is a directory with an index.php (I assume that you are assuming a DirectoryIndex index.php (is this the case for GoDaddy?)
This is all hopelessly confused. Are you hiding or exposing the .php extension? Because Rule 3,5 and 6 are inconsistent. And rule 5 would seem more logical as
RewriteRule ^(.*?)/staff.php/([a-zA-Z0-9\-_]+) $1/staff.php?s=$2 [L]
Go back to the drawing board and work out what you are trying to do with your htaccess rules; what you want your public URI grammar to be; how your scripts are laid out; what redirects you want to pass back to the client browser and which you want Apache to handle as internal rewrites and what extra conditions are needed to prevent looping and misfiring. Make sure this makes sense and then debug them by building up your .htaccess file one rule at a time and using test requests to exercise each rule in turn to validate what its doing.
Trying adding the following at the start of your htaccess file. I had the same problem getting rewrites to work on GoDaddy which worked everywhere else:
Options -Multiviews
I'm currently using the following to rewrite http://www.site.com/index.php/test/ to also work directly with http://www.site.com/test/, but I would like to not only allow the second version, I would like to FORCE the second version. If a user goes to http://www.site.com/index.php/test/ it should immediately reroute them to http://www.site.com/test/. index.php should never appear in a url. Stipulation: this should only apply to the first index.php. If I have a title like http://www.site.com/index.php/2011/06/08/remove-index.php-from-urls/ it should leave the second index.php, as it is part of the URL.
Current rule that allows but does not force:
#Remove index.php
RewriteCond $1 !^(index.php|images|css|js|robots.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
Thanks.
As you wrote, if a user goes to http://www.site.com/index.php/test/ this rule will imediately reroute him to http://www.site.com/test/
RedirectMatch 301 /index.php/(.*)/$ /$1
I'm not sure if that is what you need as your current rewrite rule is opposite to mine.
First (and wrong) answer - see below
You can accomplish a redirection with these directives (in this order):
RewriteCond %{REQUEST_URI} ^index.php
RewriteRule ^index\.php/(.+)$ /$1 [R,L]
RewriteCond $1 !^(index.php|images|css|js|robots.txt)
RewriteRule ^(.*?)$ /index.php/$1 [L]
That will first redirect all the requests that begin with index.php to the corresponding shortened url, then silently serve index.php/etc with the second rule.
EDIT - Please read on!
In fact, the solution above generates an infinite redirection loop, because Apache takes the following actions (let's say we request /index.php/abc):
first RewriteCond matches
Apache redirects [R], that is, generates a new HTTP request, to /abc
/abc fails first RewriteCond
/abc matches second RewriteCond
Apache does not redirect, but rewrites this URI (so it makes an "hidden" request), to /index.php/abc . We are again at point 1, that's a loop.
Please note...
By using the [L] (last rule) flag, we can only tell Apache not to process more rewrite rules, but only if the current rule matches. Since a new HTTP request is made, there is no information about how may redirection we have been through yet. So, any time one of the two matches, and in any case it generates a new request (=>loop)
Using the [C] (chain rules) flag is kinda pointless because it makes Apache process a rule only if the previous rule matches, while the two rules we have are mutually excluding.
Using the [NS] (not if subrequest) flag on rule #1 is again not an option because it aƬsimply does not apply to our case (see Apache RewriteRule docs about it)
Setting env variables is not an option (alas), since a new request is made at pt 2, thus destroying all environment variables we set.
An alternative solution can be to rewrite e.g. /abc , to /index.php?path=abc. That is done by these rules (please, delete your RedirectMatch similar rule before adding these):
RedirectMatch ^/index\.php(/.*) $1
RewriteCond %{REQUEST_URI} !^/(index.php|images|css|js|robots.txt|favicon.ico)
RewriteRule ^(.+) /index.php?path=$1 [L,QSA]
I don't know the internals of CodeIgniter's scripts, but as most of the MVC scripts, it will read $_REQUEST['PATH_INFO'] to understand which page is requested. You could slightly modify the code that recognizes the page like this (I assumed that the page path is stored in the $page var):
$page = $_REQUEST['PATH_INFO'];
if(isset($_GET['path']) && strlen($_GET['path'])) $page = $_GET['path']; // Add this line
This won't break the previous code and accomplish what you asked for.