virtual subdomain - .htaccess

i want this:
http://100.mydomain.ir ---> http://mydomain.ir?option=user&id=100
i try this, but do not work:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} !^(www|mail).mydomain.ir$ [NC]
RewriteCond %{HTTP_HOST} !^(www|mail).mydomain.ir$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([a-z0-9-]+).mydomain.ir$ [NC]
RewriteRule ^(.*)/$ ?option=user&id=%2 [R=301,L]
what is my correct .htaccess code?

Use mod_rewrite to achieve this.

Related

Redirect from sub.domain.co.id to https://sub.domain.co.id

Good day. I have a website called with sub.domain.co.id and now I want to redirect it to https://sub.domain.co.id.
I'm using codeigniter and this what i change in my .htaccess
Before .htaccess look like this
RewriteEngine on
RewriteCond $1 !^(index\.php|public|\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1
then I change it to ref(redirecting https://domain.com to https://www.domain.com)
RewriteEngine on
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^http://. [NC]
RewriteRule ^ https://sub.domain.co.id%{REQUEST_URI} [R=301,L,NE]
RewriteCond $1 !^(index\.php|public|\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1
but no help. the URL still use http. How can i achieve that?
try this and see. (index.php will removed too)
RewriteEngine on
RewriteCond $1 !^(index\.php|assets|image|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
RewriteCond %{HTTP_HOST} ^sub.domain.co.id [NC]
RewriteRule ^(.*)$ https://www.sub.domain.co.id/$1 [L,R=301]
didn't test. 95% Sure. Not the 100%

force http to https with an egsisting acces file

I dont know much about htaccess files and i was hoping you can help me.
I have this file:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?d-hive.net$
RewriteCond %{REQUEST_URI} !^/d-hive/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /d-hive/$1
RewriteCond %{HTTP_HOST} ^(www.)?d-hive.net$
RewriteRule ^(/)?$ d-hive/index.php [L]
I was wondering how can i force the page to open up the HTTPS protocol instead of the HTTP protocol.
use full url e.g.
RewriteRule ^(/)?$ https://www.d-hive.net/index.php [L]
Plus, can include
RewriteCond %{HTTPS} =on [NC]
so something like,
RewriteCond %{HTTPS} =on [NC]
RewriteRule ^(/)?$ https://www.d-hive.net/index.php [L]
You can have your rules like this. Let me know how this works for.
RewriteEngine on
RewriteCond %{HTTPS} !^on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTP_HOST} ^(www\.)?d-hive\.net$
RewriteCond %{REQUEST_URI} !^/d-hive/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /d-hive/$1
RewriteCond %{HTTP_HOST} ^(www\.)?d-hive\.net$
RewriteRule ^(/)?$ d-hive/index.php [L]

RewriteCond blank domain to the domain with the correct path

How do I set redirects with following conditions:
Url http://domainname.be becomes http://www.domainname.be/nl/
Url http://www.domainname.be becomes http://www.domainname.be/nl/
Url http://domainname.be/nl/custompage/ becomes http://www.domainname.be/nl/custompage/
?
Rewrite conditions
RewriteCond %{HTTP_HOST} ^domainname.be [NC]
RedirectRule ^(.*)$ http://www.domainname.be$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^domainname.be [NC]
RewriteCond %{REQUEST_URI} ^/$
RedirectRule ^(.*)$ http://www.domainname.be/nl$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^www.domainname.be [NC]
RewriteCond %{REQUEST_URI} ^/$
RedirectRule ^(.*)$ http://www.domainname.be/nl$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/nl/custompage /Custompage.aspx?lang=nl [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/nl/custompage/ /Custompage.aspx?lang=nl [L,NC]
But is doesn't work. Also, do you need to clear the browsercache in order to have the latest conditions?
Remove all of your existing rules and use this:
RewriteCond %{HTTP_HOST} ^(www\.)?domainname\.be$ [NC]
RedirectRule ^((?!nl/).*)$ http://www.domainname.be/nl/$1 [R=301,L]
And yes test it out in a different browser to avoid 301 caching issues.

Subdomain rewrite .htaccess

I have the follow .htaccess and it work good.
from http://us.domain.org/detail.php?id=1234 to
http://us.domain.org/category/city/computer-1234
but if I add:
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^[^.]+\.domain\.org$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ /detail.php?id=$1 [L,QSA]
It does not work..How can I do?
from http://us.domain.org/detail.php?id=1234 to http://us.domain.org/category/city/computer-1234
Change your last rule to:
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^[^.]+\.domain\.org$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/([^/-]+)-([^/]+)/?$ /detail.php?id=$4 [L,QSA]

Htaccess redirect question

I have a site I've written in php.
The addresses are as follows:
http://www.site.com/page.php
I'd like any request to:
www.site.com/page.php
or
www.site.com/page
To go to:
www.site.com/page/
(And force a www.)
Can anyone help me?
For domain :
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]
For your first directory :
RewriteRule ^/([^/]*)(\.php)? /$1/
RewriteRule ^page.php$ http://www.site.com/page/ [QSA,L]
RewriteRule ^page$ http://www.site.com/page/ [QSA,L]
Maybe this will work.
This should do what you wanted:
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]+\s/([^\s]*?)\.php [OR]
RewriteCond %{THE_REQUEST} ^[A-Z]+\s/([^\s]*?[^/\s])\s [OR]
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST}/%1 ^(www\.)?(.*?)/?$
RewriteRule ^ http://www.%2/ [R=301,L]
# Assuming you want to write the request back to page.php too...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)/$ $1.php
Tim's answer is the closest, but it also does adds the trailing slash to images and css...
So taking Tim's answer, and slightly editing it gives us:
RewriteEngine on
RewriteRule \.(css|jpe?g|gif|png)$ - [L]
RewriteCond %{THE_REQUEST} ^[A-Z]+\s/([^\s]*?)\.php [OR]
RewriteCond %{THE_REQUEST} ^[A-Z]+\s/([^\s]*?[^/\s])\s [OR]
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST}/%1 ^(www\.)?(.*?)/?$
RewriteRule ^ http://www.%2/ [R=301,L]
# Assuming you want to write the request back to page.php too...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)/$ $1.php
Which should work!

Resources