URL Rewriting Problem - .htaccess

I'm trying to rewrite my URL's subdomain.
http://username.domain.com >>> http://www.domain.com/user.php?u=username
I'm using this for my .htaccess
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/index\.php$
RewriteCond %{HTTP_HOST} !^www\.domain\.com
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com
RewriteRule .* /user.php?u=%1 [L]
Can you help me for debugging this problem ?
(Username's can contain a-z 0-9 and hypens)
Also if subdomain is www or api, don't redirect them

Try this one
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/index\.php$
RewriteCond %{HTTP_HOST} !^www\.domain\.com
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com
RewriteRule ^(.*)$ /user.php?u=%1 [L]
I have the same use case and its working for me
EDIT:
Add this to your httpd.conf file and try debugging
<IfModule mod_rewrite.c>
RewriteLog "F:/wamp/www/logs/rewrite.log"
RewriteLogLevel 3
</IfModule>

Related

.htaccess Prevent from running in parallel across multiple domains

I have two domains:
www.mywebsite.com
www.my-website.com
I want all traffic going to my-website.com (no www.)
In the .htaccess for www.mywebsite.com I currently have the following:
RewriteCond %{HTTP_HOST} ^mywebsite\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.mywebsite\.com$
RewriteRule ^/?$ "http\:\/\/www\.my\-website\.com\/" [R=301,L]
In the .htaccess for www.my-website.com I currently have the following:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^111\.111\.111\.111
RewriteRule (.*) http://www.my-website.com/$1 [R=301,L]
I am looking for the correct way to write both .htaccess files so everything goes to my-website.com and prevent from running in parallel.
in www.mywebsite.com .htaccess I entered the following:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^mywebsite.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.mywebsite.com$
RewriteRule (.*)$ http://my-website.com/$1 [R=301,L]
</IfModule>
in www.my-website.com I entered the following:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^my-website\.com
RewriteRule (.*) http://my-website.com/$1 [R=301,L]

.htaccess & mod_rewrite redirect issues

Alright. I'm having no luck. So I figured I'd see if you all could help make some sense of this issue.
I've inherited a site that previously had a lot of different subdomains, which have now all been written into the .htaccess file to rewrite as subdirectories (i.e., blog.site.com becomes site.com/blog).
To avoid duplicate content issues, I also need to rewrite all site.com URL's as www.site.com. And even though the current rules look good to me, they're apparently not rewriting all the subdirectories of the domain. (I've never been that great with RegEx, so that could be part of the problem.)
Here are the current rules from .htaccess:
#---------------------------------
# Start rewrite engine
#---------------------------------
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
</IfModule>
<IfModule mod_rewrite.c>
RewriteCond %{HTTPS} !=on
RewriteCond %{PATH_INFO} !^$
RewriteCond %{HTTP_HOST} !^localhost$ [NC]
RewriteCond %{HTTP_HOST} !^www\..+$ [NC]
RewriteCond %{HTTP_HOST} !^blog\..+$ [NC]
RewriteCond %{HTTP_HOST} !^docs\..+$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</IfModule>
#---------------------------------
# Rewrite some of our subdomains
#---------------------------------
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^blog\.site\.com$ [NC]
RewriteRule (.*)$ "http://www.site.com/blog/$1" [R=301,QSA,L]
RewriteCond %{HTTP_HOST} ^docs\.site\.com$ [NC]
RewriteRule (.*)$ http://www.site.com/docs/$1 [R=301,QSA,L]
RewriteCond %{HTTP_HOST} ^wiki\.site\.com$ [NC]
RewriteRule (.*)$ http://www.site.com/wiki/$1 [R=301,L]
</IfModule>
#---------------------------------
# Wordpress specific - do not place anything below this line
#---------------------------------
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
(Obviously, some of them exist to allow for a local dev environment, etc).
Any thoughts on why I'm not seeing site.com/directory rewritten as www.site.com/directory? What am I missing here?

htaccess redirect subdomain

I have the following .htaccess code:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.mysite.co$ [NC]
RewriteRule ^(.*)$ http://www.mysite.co/$1 [R=301,QSA,L]
</IfModule>
The problem is that all subdomains get redirected to www.mysite.co/subdomain
How can I aviod this?
Your rewrite rule logic in plain speak is doing the following:
For ANY HOST other thant www.mysite.co (including foo.mysite.com, blog.mysite.com, etc..)
Permanent Redirect (301) to http://www.mysite.co/
This is the last rule to check in the .htaccess file, so bail out
To not redirect your own subdomains, the easiest and clearest way is to handle it explicitly with more rewrite conditions (see example below). For more kung fu htaccess fun try this: .htaccess Wildcard Subdomains
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.mysite.co$ [NC]
RewriteCond %{HTTP_HOST} !^blog.mysite.co$ [NC]
RewriteCond %{HTTP_HOST} !^foo.mysite.co$ [NC]
RewriteRule ^(.*)$ http://www.mysite.co/$1 [R=301,QSA,L]
</IfModule>
You need to run your index.php file
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([a-z0-9-]+)\.example\.com$ [NC]
RewriteRule !^index\.php($|/) index.php/accounts/%2%{REQUEST_URI} [PT,L]

moving to .htaccess

I moved my site to a new php 5.2 server with no access to apache conf files
So I had to put site's rewrite rules into an .htaccess (wildcards is open for the domain)
and I expirience a lot of issues with it now
ORIGINAL WAS
Options +FollowSymLinks
rewriteEngine on
rewriteCond $1 !^pages/
rewriteCond %{HTTP_HOST} !^www\.mydomain\.com
rewriteCond %{HTTP_HOST} ^([^.]+)\.mydomain\.com
rewriteRule (.*) /pages/%1/$1
rewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /pages/
rewriteRule ^pages/([^/]+)/(.*)$ http://$1.mydomain.com/$1 [R=301,L]
What changes Do I need to make ?
the whole point is that I need to redirect:
www.mydomain.com/pages/XXXXX.php ==>
XXXXX.mydomain.com/...whatever....
but also
www.mydomain.com/pages/XXXXX/...whatever.... ==> XXXXX.mydomain.com/...whatever....
The rules below should do what you requested.
However, in the first example, where does the whatever appear in the original request to www.mydomain.com/pages/XXXXX.php.
If the whatever refers to the query string, then it should work.
RewriteCond %{HTTP_HOST} ^www\.mydomain\.com$ [NC]
#redirect www.mydomain.com/pages/XXXXX.php to XXXXX.mydomain.com/
RewriteRule ^pages/(.+)\.php http://$1.mydomain.com/ [R=301,L]
RewriteCond %{HTTP_HOST} ^www\.mydomain\.com$ [NC]
#redirect www.mydomain.com/pages/XXXXX/.whatever.. to XXXXX.mydomain.com/...whatever
RewriteRule ^pages/([^/]+)/(.*) http://$1.mydomain.com/$2 [R=301,L]
You could try the following:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} ^pages/
RewriteRule ^pages/([^/]+)(?:\.php|/(.*))$ http://$1.mydomain.com/$2 [R=301,L]
RewriteCond %{REQUEST_URI} !^pages/
RewriteCond %{HTTP_HOST} !^www\.mydomain\.com
RewriteCond %{HTTP_HOST} ^([^.]+)\.mydomain\.com
RewriteRule (.*) /pages/%1/$1 [L]
</IfModule>
And one more: please make sure whether you REALLY NEED the Options +FollowSymLinks directive as this might cause an internal error if not allowed or already set in httpd.conf

What is wrong with these rewrite rules

What is wrong with this code, I thought it was working but it appears not to be now, any help?
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^forum.ohmsgaming.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.forum.ohmsgaming.com$
RewriteRule ^/?$ "http\:\/\/ohmsgaming\.com\/community\/forum\/" [R=301,L]
</IfModule>
<IfModule mod_rewrite.c>
RewriteCond %{HTTP_HOST} ^outhousemouse.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.outhousemouse.com$
RewriteRule ^/?$ "http\:\/\/ohmsgaming\.com" [R=301,L]
</IfModule>
You don't need to escape the RewriteRule values. That might be your problem.
Example:
RewriteRule ^/?$ http://ohmsgaming.com/community/forum/ [R=301,L]
When in doubt, turn on rewrite logging:
RewriteLog /var/log/apache2/MYDOMAIN_rewrite.log
RewriteLogLevel 5
This log can be viewed using tail -f /path/to/log, then reload the page. It will clearly list the processing going on.
Edit
I just noticed that the conditions are not escaped, they should look like:
RewriteCond %{HTTP_HOST} ^forum\.ohmsgaming\.com$

Resources