I have the following rule in my htaccess, I need to modify it to add variable to the end of the re-written url. Basically I have two domains pointing to the same hosting, and I am showing different sites depending on which domain it used.
RewriteCond %{HTTP_HOST} ^(www\.)?rrr\.me
RewriteCond %{REQUEST_URI} !^/rrr
Rewriterule ^(.*)$ /rrr/$1 [L]
If someone goes to rrr.me or www.rrr.me they get the index.php in the rrr directory of my website. This works great!
I now need to be able to add a variable to the index.php. So someone will type in rrr.me/abc and it will append the abc to the end of the index.php in this fashion index.php?var1=abc
NEW REWRITE RULES AFTER ADVICE FROM Olaf Dietsche IN COMMENTS.
RewriteCond %{HTTP_HOST} ^(www\.)?rrr\.me
RewriteCond %{REQUEST_URI} !^/rrr
RewriteCond %{REQUEST_URI} !/assets/
Rewriterule ^.*$ /rrr/index.php?rrr=$0 [L]
RewriteCond %{HTTP_HOST} ^(www\.)?rrr\.me
RewriteCond %{REQUEST_URI} !^/rrr
Rewriterule ^assets/.*$ /rrr/$0 [L]
To append the requested URL as a query string
Rewriterule ^.*$ /rrr/index.php?var1=$0 [L]
To exclude the assets directory, you must use an additional RewriteCond
RewriteCond %{HTTP_HOST} ^(www\.)?rrr\.me
RewriteCond %{REQUEST_URI} !^/rrr
RewriteCond %{REQUEST_URI} !/assets/
Rewriterule ^(.*)$ /rrr/$1 [L]
For the assets files, you can use a similar redirect
RewriteCond %{HTTP_HOST} ^(www\.)?rrr\.me
RewriteCond %{REQUEST_URI} !^/rrr
Rewriterule ^assets/.*$ /rrr/$0 [L]
Related
I want to redirect a path to a file as query parameter.
For ex:
http://subdomain.example.com/helloworld/
Should redirected to
/index.php?domainParam=subdomain¶m1=helloworld
I have tried multiple ways but they either helps with subdomain or with the path. But I want both of them to be redirected as a parameter in a query string.
Like:
RewriteCond %{HTTP_HOST} !^app\.example\.com$ [NC]
RewriteCond %{HTTP_HOST} ^([^\.]+)\.example\.com$ [NC]
RewriteRule ^$ index.php?domainParam=%1 [L]
RewriteCond %{HTTP_HOST} !^app\.example\.com$ [NC]
RewriteRule ^([^/]+)/? index.php?source=$1 [L]
But it is not helping when redirecting both of them as parameters.
Need your assistance to merge them both.
You may use this rule for this task:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} ^(?!app\.)([^.]+)\.example\.com$ [NC]
RewriteRule .* index.php?domainParam=%1¶m1=$0 [L,QSA]
This is my current htaccess.
Site
|.htaccess
|--/folder-1
|--/folder-2
Now my root htaccess is as follows:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^site.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^site.com$
RewriteRule ^/?$ /public/ [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/public/
RewriteRule ^(.*) /public/$1 [L]
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
I want users to access all files and folders inside folder 1 and folder 2. How that can be done?
Several changes: do your domain redirect first (note the subtle changes);
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
RewriteRule .* http://%1/$0 [R=301,L]
RewriteCond %{HTTP_HOST} ^site\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(?!(?:folder-1|folder-2)(?:$|/)|public/(?:index\.php)?$).* public/$0 [L]
This incorporates the folders you're excluding from rewriting into the main rule, so should be more efficient. It looks ugly since it starts with a negative look-ahead assertion (?! and the rest of the groups begin with (?: so they are non-capturing.
Do you really need a single domain? If so, it should be checked on all requests. If not, drop the first RewriteCond of the second RewriteRule.
if your default document is not index.php, change it in the negative look-ahead assertion of the second RewriteRule.
I have the following .htaccess, which is not working as expected:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^example.bg$
RewriteRule (.*) http://www.example.bg/$1 [R=301,L]
RewriteCond %{REQUEST_URI} !^/sessiontest\.php$
RewriteCond %{REQUEST_URI} ^/login.* [OR]
RewriteCond %{REQUEST_URI} ^/admin.*$
RewriteRule ^(.*)$ travelpartner.php [L,NC]
RewriteCond %{REQUEST_URI} !^/sessiontest\.php$
RewriteCond %{REQUEST_URI} !^/login.*$
RewriteCond %{REQUEST_URI} !^/admin.*$
RewriteRule ^(.*)$ perla/$1 [L,NC]
What I need is the following:
If the URI is missing the "www" from example.bg, then add it and redirect to www.example.bg
If the URI does not end in "sessiontest.php" and starts with "www.example.bg/login" or starts with www.example.bg/admin, then forward all requets to travelpartner.php
For all other cases, we should forward all requests to a subfolder, called "perla"
However, right now what happens is that we always see the contents of the perla subfolder, even if I open exemple.bg/login
What am I missing here?
With your current rules I get the infinite redirect loop:
Once the rule No.2 is matched, and the request /login is rewritten to /travelpartner.php, the rewritten request is handed back to the URL parsing engine and the ruleset is run again from the start.
This time, it will match the last segment and be rewritten to /perla/travelpartner.php,and sent back again, and be rewritten to /perla/perla/travelpartner.php, etc...
The fix is to, in the last segment, prevent rewriting if the request starts with /perla or is /travelpartner.php:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^example.bg$
RewriteRule (.*) http://www.example.bg/$1 [R=301,L]
RewriteCond %{REQUEST_URI} !^/sessiontest\.php$
RewriteCond %{REQUEST_URI} ^/login [OR]
RewriteCond %{REQUEST_URI} ^/admin
RewriteRule ^ travelpartner.php [L,NC]
RewriteCond %{REQUEST_URI} !^/sessiontest\.php$
RewriteCond %{REQUEST_URI} !^/login
RewriteCond %{REQUEST_URI} !^/admin
RewriteCond %{REQUEST_URI} !^/perla/
RewriteCond %{REQUEST_URI} !^/travelpartner\.php$
RewriteRule ^(.*)$ perla/$1 [L,NC]
You can use the following rules
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.bg$
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R]
RewriteRule ^(login|admin) /travelpartner.php [L]
RewriteRule !(perla/|travelpartner\.php) /perla [L]
i have many rules in my .htaccess file.
# project1.mysite.com
RewriteCond %{HTTP_HOST} project1.mysite.com$
RewriteCond %{REQUEST_URI} !^/project1
RewriteRule ^(.*)$ /project1/$1 [L]
# project554.mysite.com
RewriteCond %{HTTP_HOST} project554.mysite.com$
RewriteCond %{REQUEST_URI} !^/project554
RewriteRule ^(.*)$ /project554/$1 [L]
# project44.mysite.com
RewriteCond %{HTTP_HOST} project44.mysite.com$
RewriteCond %{REQUEST_URI} !^/project44
RewriteRule ^(.*)$ /project44/$1 [L]
e.t.c
i think what there is more smart rule by using only one mask.
subdomain like project{number} -> to folder like /project{number}
please advise a good solution
RewriteCond %{HTTP_HOST} (project\d+).mysite.com$
RewriteCond %{REQUEST_URI} !=/%1
RewriteRule ^(.*)$ /%1/$1 [L]
This should work as expected. Remove everything else from the file.
I am running wildcard subdomains. the aim of the site is to run a virtual subdomain. what the functionality should be is that when the files
from the main site are called they will be rewritten for the clean URLs. But when i am calling the same files form the subdomain
like xyz.doamin.com/my-file.php then this should be rewritten like it is calling that file with an argument of subdoamin like
domain.com/my-file.php?var=xyz. Currently what i have done for the file call is this
Options +FollowSymLinks
RewriteEngine on
## Redirecting all non-www domains to www.domain.com
RewriteCond %{HTTP_HOST} ^domain.com [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,NC,L]
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com$ [NC]
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^(.*)$ http://www.domain.com%{REQUEST_URI}?var=%1 [L]
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com$ [NC]
RewriteCond %{QUERY_STRING} !^$
RewriteRule ^(.*)$ http://www.domain.com%{REQUEST_URI}?%{QUERY_STRING}&var=%1 [L]
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com$ [NC]
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^(.*)$ http://www.domain.com/index.php?subdomain=%1 [L]
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com$ [NC]
RewriteCond %{QUERY_STRING} !^$
RewriteRule ^(.*)$ http://www.domain.com%{REQUEST_URI}?%{QUERY_STRING}&subdomain=%1 [L]
This is throwing a 500 internal error for the file call.
Rule set 4 will overlap with rule set 1 and 2. I suspect this is causing the issue.
So I think all 3 need an extra RewriteCond check on REQUEST_URI.
Really you shouldn't need rule set 4. Just including index.php as a default should do the trick. Unless of course you don't want URL's like http://www.domain.com/?var=xyz, in this case you need rule 4, but you need to split it into 2 - one with a QUERY_STRING and one without, just like rule 1 and 2
Also, isn't %1 from the regex in the rewriteRule itself and not the preceeding rewriteConds? If so then your regex is wrong, because it'll be setting the var= to the entire original URL.
Looking in the error log is a good idea as it will give you the reason for the 500 error.
Edit: Ok, try something like this (not tested)
## Redirecting all non-www domains to www.domain.com
RewriteCond %{HTTP_HOST} ^domain.com [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L,QSA]
## Redirecting / requests to index.php
# Note: Use the QSA flag for handling with or without query string
RewriteCond %{REQUEST_URI} ^/$
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/index.php?var=%1 [L,QSA]
## Redirecting /something requests to index.php
# Note: Use the QSA flag for handling with or without query string
RewriteCond %{REQUEST_URI} ^/.+$
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com%{REQUEST_URI}?var=%1 [L,QSA]