Conditional htaccess - php

I just want some help with my .htaccess configuration. Before I have this problem where I need to redirect the page to
a specific url. Like if the user type http://mydomain/test it will be redirected to http://mydomain/app/r/test. I
achieved the redirection using .htaccess. The code is below
RewriteEngine on
RewriteRule (.*) http://mydomain/app/r/$1 [R=301,L]
But I have this new specification. As follows:
If user visit http://mydomain/ or http://domain/index.php, he will be
redirected to http://mydomain/mymanager
If user visit
http://mydomain/{any_segment}, he will be redirected to
http://mydomain/app/r/{any_segment}
Here is my current htaccess. Basically in the url, any characters will be redirected to /app/r/{} except index.php. But what about the http://domain/? How can I achieve this.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^mydomain$ [OR]
RewriteCond %{HTTP_HOST} ^www.mydomain$
RewriteCond %{REQUEST_URI} !^/index.php
RewriteRule ^(.*)$ http://mydomain/app/r/$1 [R=301,L]
I've been playing around my htaccess and any pointers will be of great help to me.

These are your rules :
RewriteEngine on
RewriteCond %{HTTP_HOST} ^mydomain$ [OR]
RewriteCond %{HTTP_HOST} ^www.mydomain$
RewriteCond %{REQUEST_URI} !^/index.php
RewriteRule ^(.*)$ http://mydomain/app/r/$1 [R=301,L]
It looks like , any request should redirect to /app/r/ except A URI start with /index.php but still not doing that , in normal cases , so you should exclude the target directory from being redirect again and again by this rule RewriteRule ^(.*)$ http://mydomain/app/r/$1 [R=301,L] , ^(.*)$ means any URI start with any thing and the server will look, if there is a condition , and actually there is only one condition to exclude index.php, otherwise redirect .
So , you should exclude this target directory along with index.php like this :
RewriteCond %{REQUEST_URI} !^(/index\.php|/app/r/)
But still you want to match a request that target HOST only so,you should add that as well :
RewriteCond %{REQUEST_URI} !^(/index\.php|/app/r/|/?$)
Then you could match host with www or non-www like this :
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain$
Your rules should look like this :
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain$
RewriteCond %{REQUEST_URI} !^(/index\.php|/app/r/|/?$)
RewriteRule ^(.*)$ http://mydomain/app/r/$1 [R=301,L]
Also you could summarize that more by utilizing %{REQUEST_URI} like this :
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain$
RewriteRule !^(index\.php|app/r/|/?$) http://mydomain/app/r%{REQUEST_URI} [R=301,L]
NOTE: Clear browser cache then test

Related

Redirect sub domain to sub directory

I want to redirect the the subdomain to subdirectory but not working. Here is my efforts.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^name\.site_url
RewriteRule ^(.*)$ http://site_url/name/$1 [L,R=301]
Try this:
RewriteCond %{HTTP_HOST} ^sub\.
RewriteRule ^(.*)$ http://your_domain/sub/$1 [R,L]
Instead sub in RewriteCond and RewriteRule you can place whatever you want
Btw, RewriteCond %{HTTP_HOST} ^sub\.my\.domain$ also works for me. So, check your site url. Or give more info (at least, what happened in apache error.log).

PHP Mod Rewrite

I have a .htaccess which does a basic rewrite which looks like the following:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php?url=$1 [L,QSA]
The next condition is to remove www. from the URL and looks like this:
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
The problem is, when I call a link such as:
http://somesite.com/category/subcategory/?id=123
and add the www. manually, it rewrites the URL to this:
http://www.somesite.com?url=category/subcategory/?123
The page stills load but, I've been told that's terrible for SEO. Any thoughts of how to fix this?
Your current issue is that you have your WWW redirect after your main SEO rules, this is what happens behind the scenes:
You access http://somesite.com/category/subcategory/?id=123
Your rules internally redirect it to index.php?url=category/subcategory/?id=123
Your last rule to redirect without the www will also take place and will ended up redirecting it to:
http://somesite.com/?url=category/subcategory/?id=123
In order to fix that you would need your rules as follow:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
RewriteRule ^index.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php?url=$1 [L,QSA]
Which leads us to your second issue, you've been using 301 redirects so your browser have cached some redirects and you will need to use a different browser temporarily to test your changes, preferable one you haven't used yet to access that site, while you clear the cache of that browser and wait for it to completely clear out.
Once the cache of your default browser has cleared you can use it as usual and you should get the same response.

URL Rewriting domain to use subdirectory as root; still shows that directory in URL in Analytics?

I set up a certain domain to rewrite into a folder, shown below, but have been having people show up in that folder in Google Analytics.
in short: /c/ -> root when from domain, but GA still shows domain/c/ in Visitors Flow
root .htaccess:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?certain-domain.com$ [NC]
RewriteCond %{REQUEST_URI} !c/ [NC]
RewriteRule ^(.*)$ /c/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteCond %{REQUEST_FILENAME}\.png -f
RewriteRule ^(.*)$ $1.png
RewriteCond %{REQUEST_FILENAME}\.txt -f
RewriteRule ^(.*)$ $1.txt
Options -Indexes
and /c/'s .htaccess is just an edited html5boilerplate one with the only difference being the same php and txt rewrite above.
But on Google Analytics, more than half of my visitors are still ending up on /c/index.php.
Visitor Flow: http://i.imgur.com/JwtcTFI.png
I haven't seen /c/ in my URL, so I really don't have a clue what's going on.
Any ideas? I do use relative URLs (is this a bad idea?) so that causes the flow to continue in the /c/ folder.
Thanks for the help!
Your rules do nothing to prevent people from directly accessing /c/whatever. If you want to prevent that, you need to add an extra rule:
RewriteCond %{THE_REQUEST} \ /c/
RewriteRule ^c/(.*)$ /$1 [L,R=301]
Oo, I found the answer! It seems like www.domain was redirecting to domain/c/index.php. I had a rewrite rule to remove the "www" in the /c/.htaccess, but not the root .htaccess.
I fixed it by adding code to remove the "www" in the root htaccess too BEFORE the domain>>directory rewrite. (after didn't seem to work)
I added
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
above
RewriteCond %{HTTP_HOST} ^(www.)?certain-domain.com$ [NC]
RewriteCond %{REQUEST_URI} !c/ [NC]
RewriteRule ^(.*)$ /c/$1 [L]
(in the root htaccess)
edit: It also probably has to do with the (www.), I dropped that due to redundancy. Now it's just RewriteCond %{HTTP_HOST} ^certain-domain.com$ [NC]
I also added a rewrite for /c/ to root in the /c/ .htaccess:
RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /c/
RewriteRule ^(.*)$ /$1 [L,R=301]

Send subdomain as post data

Having spent the past few hours looking for a solution, I thought I'd ask here.
I'm looking for a way to process links such as:
http://subdomain.domain.com/page/subpage/
so they are passed to the server as:
http://domain.com/index.php?subdomain=subdomain&page=page&subpage=subpage
which can be taken by index.php to display the correct page information.
I can get the page and subpage to work but have no clue how to get the subdomain. Note: if there is no subdomain, I would like that field to either be left blank or contain a default value.
Any assistance would be much appreciated. :)
Edit:
Just to clarify, I want the first URL to be what the user sees and can input into the address bar and the second URL to be the page that is loaded by the server when the user navigates to the first URL.
My .htaccess file
Options +FollowSymlinks -MultiViews -Indexes
RewriteEngine On
RewriteBase /
# Remove 'www'
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
# Add slashes
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !index.php
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://resolutiongaming.com/$1/ [L,R=301]
# Subdomain redirect
RewriteCond %{HTTP_HOST} ^resolutiongaming.com$
RewriteRule ^webdev/$ http://webdev.resolutiongaming.com [R=301,L]
RewriteRule ^artwork/$ http://artwork.resolutiongaming.com [R=301,L]
RewriteRule ^music/$ http://music.resolutiongaming.com [R=301,L]
ErrorDocument 404 /error.php
You may try this in one .htacces file in root directory:
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(?:www\.)?([^\.]+)\.domain\.com
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/([^/]+)/([^/]+)/? [NC]
RewriteRule .* http://domain.com/index.php?subdomain=%1&page=%2&subpage=%3 [R=301,L]
It will redirect permanently:
http://subdomain.domain.com/page/subpage/ or http://www.subdomain.domain.com/page/subpage/
To:
http://domain.com/index.php?subdomain=subdomain&page=page&subpage=subpage
For the rule to work, the incoming URL scheme: /page/subpage/ must be kept.
To replace permanent redirection with silent mapping, remove R=301 from [R=301,L] or replace it with R for temporal redirection.

RewriteRule violates while switching from https to http

I write a lot of RewriteRule in my .htaccess file , but problem occurs when I switch from https to http pages; it does not follow these rules
NOTE : everything working fine on localhost , issues are on server <---- UPDATE
Here is my website , currently all links display as per RewriteRule*
for e.g. about us page link display as
http://www.mywebsite.com/about
BUT
if I am at login page ( which is on https ) and click on about us page then it turns into below.
http://www.mywebsite.com/about?slug=about_us
or if I click on any category on left panel then it comes as
http://www.mywebsite.com/auction/category/1?cid=1
Note : even mouse hovering on pages display rewrite link
below is .htaccess file with needed information.
IndexIgnore *
RewriteEngine on
Options +FollowSymLinks
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^auction/category/([0-9]+)/?$ bids.php?cid=$1 [NC]
RewriteRule ^login/?$ login.php [NC]
RewriteRule ^register/?$ register.php [NC]
RewriteRule ^logout/?$ logout.php [NC]
# static pages
RewriteRule ^about/?$ page.php?slug=about_us [NC]
# Rewrite to https
RewriteCond %{SERVER_PORT} !^443$ [OR]
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} /login [OR]
RewriteCond %{REQUEST_URI} /do_login.php
RewriteRule ^(.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,NC,L,QSA]
# don't do anything for images/css/js (leave protocol as is)
RewriteRule \.(gif|jpe?g|png|ico|css|js)$ - [NC,L]
# traffic to http:// except some pages
RewriteCond %{SERVER_PORT} ^443$ [OR]
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !(/login|/do_login.php)
RewriteRule ^(.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,NC,L,QSA]
Note : Here is complete .htaccess file
Please tell me where I am wrong/missing ?
I Also have few more confusion
If i change the case of rewrite URL ( Login or lOgin or logiN ) then it gives error?
Is it good practice to write [NC,L] with all RewriteRule?
Exactly when I should write [QSA] ?
UPDATE
After suggestions from all answers, changing in RewriteRule almost fixed all issue but now there is one last issue.
/login URL always changed into /login.php.
below is my updated .htaccess
IndexIgnore *
Options -MultiViews
Options +FollowSymLinks
#mod_rewrite
RewriteEngine on
RewriteBase /
# Rewrite www to non www
RewriteCond %{HTTP_HOST} ^www.%{HTTP_HOST} [NC]
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [R=301,L]
# minimize the css on all http:// pages
<IfModule mod_rewrite.c>
RewriteCond %{HTTPS} off
RewriteRule ^(.*).css$ /csszip.php?file=$1.css [L]
</IfModule>
#switch over http to https
# Rewrite to https
RewriteCond %{SERVER_PORT} !^443$ [OR]
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} (/login|/do_login)\.php [NC]
RewriteRule ^(.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# don't do anything for images/css/js (leave protocol as is)
RewriteRule \.(gif|jpe?g|png|ico|css|js)$ - [NC,L]
# traffic to http:// except some pages
RewriteCond %{SERVER_PORT} ^443$ [OR]
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !(/login|/do_login)\.php [NC]
RewriteRule ^(.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^login/?$ login.php [NC]
RewriteRule ^register/?$ register.php [NC]
# ...many other rules...with [NC] falg
RewriteRule ^auction/category/([^/.]+)/?$ bids.php?cid=$1 [NC]
RewriteRule ^about/?$ page.php?slug=about_us [NC]
# ...many more rules.... with [NC] flag
HTTPS suggestion
For your HTTPS issue I would match on port or HTTPS as there are known apache problems relating to the HTTPS tag.
To cover this match on both (as shown in your edited answer)
RewriteCond %{SERVER_PORT} !^443$ [OR]
RewriteCond %{HTTPS} != on
##REWRITE RULE
RewriteCond %{SERVER_PORT} ^443$ [OR]
RewriteCond %{HTTPS} = on
##REWRITE RULE
A valid point is also that %{REQUEST_URI} isn't affected by any substitutions.
The way you are using it at the moment, if any rule matches you will send them to the original url (before any substitution started).
If you want to take the url after and substition and matching use $1
Answers to your further questions:
If I change the case of rewrite URL then it gives error?
This is because your [NC] isn't on the rewrite cond for the HTTPS section of your .htaccess
You match RewriteCond %{REQUEST_URI} /login [OR] this is only looking for lower case login, if you want to accept uppercase login append NC.
Is it good practice to write [NC,L] with all RewriteRule?
No, it depends what you want to do [NC] says don't match case on this rule, if you don't want to match case on that rule (or condition) then add it.
Not matching the case with [NC] means site.com/login.php = sYte.cOm/LoGin.PHP
[L] means if this is true, stop processing everything
Exactly when I should write [QSA] ?
QSA applies when you have a ? in your substitution and you want to append toe old string to the new URL
Consider the following rule:
RewriteRule /pages/(.+) /page.php?page=$1 [QSA]
With the [QSA] flag, a request for /pages/123?one=two will be mapped to /page.php?page=123&one=two. Without the [QSA] flag, that same request will be mapped to /page.php?page=123 - that is, the existing query string will be discarded.
Do use QSA if you want to keep any additional get arguments.
Another further question
/login URL always changed into /login.php
The only way for this to happen is if you have a redirect [R=301] somewhere in your code, the only place I can see that is this section:
# Rewrite to https
RewriteCond %{SERVER_PORT} !^443$ [OR]
RewriteCond %{HTTPS} != on
RewriteCond %{REQUEST_URI} (/login|/do_login)\.php [NC]
RewriteRule ^(.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Where this will only match the following URL's
.php, /login.php, /do_login.php
I believe the culprit is as I outlined in the first response with %{REQUEST_URI}
Your code essentially says, if these conditions are met, send them to https://theurltheywentto, which is not what you want to do, you want to send them to /login.
Have you tried using (as mentioned in my https section)
RewriteRule ^(.*) https://%{HTTP_HOST}/login [R=301,L]
Or perhaps (if you have /do_login) and other options
RewriteRule ^(.*).php https://%{HTTP_HOST}/$1 [R=301,L]
Where test.com/do_login.php will become https://test.com/do_login
How about you try:
# Rewrite to https
RewriteCond %{REQUEST_URI} (/login|/do_login)\.php [NC]
RewriteCond %{HTTPS} != on
RewriteRule ^(.*)\.php https://%{HTTP_HOST}/$1 [R=301,L]
I think this might be of your help
.htaccess redirect https to http not working
and for last question this might be of your help
http://httpd.apache.org/docs/current/rewrite/flags.html and
http://www.addedbytes.com/download/mod_rewrite-cheat-sheet-v2/png/
EDIT:
You can try using
RewriteCond %{SERVER_PORT} ^443$
RewriteCond %{SERVER_PORT} !^443$
instead of
RewriteCond %{HTTPS} on [NC]
RewriteCond %{HTTPS} off [NC]
For detail
Please see this link, someone had similar problem. Please see second reply of the post
http://www.webmasterworld.com/apache/3228459.htm
you can solve this by creating the domain alias
You can't do it within .htaccess, but within a http.conf <VirtualHost> section you can use the ServerName and ServerAlias directives to accomplish this.
Your confusion over /about is due to redirecting to %{REQUEST_URI}, which isn't changed as you make substitutions. Capture the URI and use $1 if you don't really want the URI as it started uring this round of rewrite processing.

Categories