Url rewrite for a domain name - apache

I need it so when i go to mydomainname.com/page/week1 it will show mydomainname.com/page.php?month=week1. Will my code do that ?
rewriteengine on
rewritecond %{HTTP_HOST} ^www.mydomianname.com$ [OR]
rewritecond %{HTTP_HOST} ^mydomianname.com$
rewriterule ^mydomianname\.com/page/?([-A-Za-z0-9]+)/?$ "http\:\/\/mydomianname\.com\/page\.php?month=$1" [L]
When I go to mydomainame.com/page/week1 I get
the requested URL /page/week1 was not found on this server.
my current http access
rewriteengine on
rewritecond %{HTTP_HOST} ^www.fetustobaby.com$ [OR]
rewritecond %{HTTP_HOST} ^fetustobaby.com$
rewriterule ^fetustobaby\.com\/page\/([-A-Za-z0-9]*)\/?$ page.php?month=$1 [L]

Your code has some issues like RewriteRule only matches URL and host name matching is not part of RewriteRule. Here is the fixed code that you can use in your .htaccess:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?mydomianname\.com$ [NC]
RewriteRule ^page/([^/]+)/?$ /page.php?month=$1 [L,NC,QSA]

Related

.htaccess RewriteRule for https specific prefix urls

Am wanting to create the https:// protocols on just these prefix urls on my site:
dream-portal.net/index.php?page=postratingspro
and
dream-portal.net/index.php?page=paypaltest
So anything in the URL after this should also include https:// protocol. For example:
dream-portal.net/index.php?page=postratingspro;sa=blahblah;testing
How can I do this? Have tried the following, but it doesn't turn all of my other urls to http:// on the site.
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} = off
RewriteRule index.php?page=^(postratingspro|paypaltest)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=302,L]
# don't do anything for images/css/js (leave protocol as is)
RewriteRule \.(gif|jpe?g|png|css|js)$ - [NC,L]
# force http for all other URLs
RewriteCond %{HTTPS} = on
RewriteCond %{REQUEST_URI} !page=^/(postratingspro|paypaltest)\$
RewriteRule .* http://%{HTTP_HOST}%{REQUEST_URI} [R=302,L]
How can I accomplish this, what is wrong with my code for this?
Replace your code with this:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} off
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+index\.php\?page=(paypaltest|postratingspro)[&;,\s] [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTPS} on
RewriteCond %{QUERY_STRING} !(^|&)page=(paypaltest|postratingspro)([,;&]|$) [NC]
RewriteRule ^(index\.php)?$ http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NC]

Rewrite rules with query strings for subdomains with rootdomain

I'm not expert in .htaccess. I want to rewrite my news and blog url (with or without www) as:
bdnews24.com/reporter.php?u=Name
to-> bdnews24.com/Name
m.bdnews24.com/reporter.php?u=Name
to-> m.bdnews24.com/Name
img.bdnews24.com/image.php?id=791011.jpg
to-> img.bdnews24.com/791011.jpg
bdnews24.com/details.php?id=100200300
to-> bdnews24.com/100200300
My current .htaccess file is below, which is not working as I want it.
This catches all request URI and if not exist it will go to index.php?u=ANYTHING
Options +FollowSymlinks
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# AlegroCart REWRITES START
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) index.php/$1 [L,QSA]
This set of rules should do what you want, based on the information you've provided:
RewriteCond {%QUERY_STRING} ^u=(.*)
RewriteCond %{HTTP_HOST} ^(m.|www.)?bdnews.com$
RewriteRule ^reporter.php /%1?
RewriteCond {%QUERY_STRING} ^id=(.*)
RewriteCond %{HTTP_HOST} ^img.bdnews.com$
RewriteRule ^image.php /%1?
RewriteCond {%QUERY_STRING} ^id=(.*)
RewriteCond %{HTTP_HOST} ^(m.|www.)?bdnews.com$
RewriteRule ^details.php /%1?
First it matches the query string, then the host name(s), then rewrites the original query string (%1) into the new URL.

Mod Rewrite with multiple parameters

I'm trying to setup a rewrite rule so that any text in place of a subdomain will be parameter one and any text after the first forward slash will be parameter two but I'm struggling with regex and am unsure about rewrite terminology.
For example, if someone requested:
joebloggs.mydomain.com
I would like them to see:
mydomain.com/index.php?site=joebloggs
Also, if someone requested:
joebloggs.mydomain.com/contact
I would like them to see:
mydomain.com/index.php?site=joebloggs&page=contact
By "see" I mean see the page, rather than see the URL - it's a CMS-like project so you can probably see where I'm going with it. Also, I've worked out how to remove www. so that's not an issue :)
EDIT
Current .htaccess is:
RewriteEngine On
# Remove trailing slash
RewriteRule ^(.+)/$ $1 [L]
# Remove www
RewriteCond %{HTTP_HOST} ^www.richardmjenkins.com$ [NC]
RewriteRule ^(.*)$ http://richardmjenkins.com/$1 [R=301,L]
# Rewrite for site/page pair
RewriteCond %{HTTP_HOST} ^(.*)\.richardmjenkins\.com$ [NC]
RewriteCond %{REQUEST_URI} !p.php
RewriteRule ^(.+/)?([^/]*)$ p.php?s=%1&p=$2 [QSA,L,NC]
RewriteCond %{HTTP_HOST} ^(.*)\.richardmjenkins\.com$ [NC]
RewriteCond %{REQUEST_URI} !p.php
RewriteRule ^$ p.php?s=%1 [QSA,L,NC]
1. In your host panel : add a subdomain with name : * for enable all subdomains
2. this is your htaccess code :
RewriteEngine On
Options +Followsymlinks
RewriteCond %{HTTP_HOST} ^(.*)\.mydomain\.com$ [NC]
RewriteRule ^$ index.php?site=%1 [QSA,L,NC]
RewriteCond %{HTTP_HOST} ^(.*)\.mydomain\.com$ [NC]
RewriteRule ^([^/]+)/?$ index.php?site=%1&page=$1 [QSA,L,NC]
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^((?!www)[^.]+)\.(mydomain\.com)$ [NC]
RewriteRule ^$ index.php?site=%1 [L,QSA]
RewriteCond %{HTTP_HOST} ^((?!www)[^.]+)\.(mydomain\.com)$ [NC]
RewriteRule ^(.+)$ index.php?site=%1&page=%2 [L,QSA]

Redirecting from subdomain without changing URL

I want to redirect a subdomain with the .htaccess file without changing the URL. But can't figure it out.
ex. i want to turn
http://foo.domain.com
into
http://www.domain.com/users/foo/dashboard.php
I generated a script to do this, but the URL doesn't stay on http://foo.domain.com
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com [NC]
RewriteRule ^ http://www.domain.com/users/%1/dashboard.php [L]
Thanks for the help!
What you're htaccess it is doing is simply saying "if you find this, do a full redirect to that url"
Do this:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com [NC]
RewriteRule ^ dashboard.php?domain=%1 [L, QSA]
And it should work (moving to wherever dashboard is in your directory structure on-server, relatively)
Try this (assuming that all the subdomains point to the root directory) :
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com$ [NC]
RewriteRule ^.*$ users/%1/dashboard.php [L]
Proxy flag [P] will do the trick:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
Options +Indexes
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.example\.org$ [NC]
RewriteRule http://example.org/users/%1/dashboard.php [P,NC,QSA]
</IfModule>

How to configure .htaccess in this way?

I have installed wildcards successfully. And I managed to make dynamic subdomain to work with a simple trick.
.htaccess:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
# *.example.com
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteCond %{HTTP_HOST} ^([a-zA-Z0-9]+)\.example\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/?subdomain\.php$
RewriteRule .? subdomain.php?subdomain=%1&path=%{REQUEST_URI} [L,QSA]
Currently in something.example.com is shown the output of subdomain.php, great.
But I want to have it working as I can enter at
pre102.example.com/folder/anotherfolder/script.php?param=1
and show the output of
example.com/folder/anotherfolder/script.php?param=1&pre=102
RewriteCond %{QUERY_STRING} !pre=[0-9]
RewriteCond %{HTTP_HOST} ^pre([0-9]+)\.example\.com$ [NC]
RewriteRule ^(.*)$ /$1?pre=%1 [L,QSA]