My .htaccess in a folder looks like:
RewriteEngine On
RewriteBase /profile/
RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?username=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ index.php?username=$1
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/profile/$1 [R=301,L]
Basically, if you go to www.mySite.com/profile/Username, my index.php file takes 'Username' as a $_GET variable, and the URL will look clean (www.mySite.com/profile/Username)
However if you go to mySite.com/profile/username (omitting the www), the URL will look like http://www.mySite.com/profile/index.php?username=username
How can I make it so only the www is added without messing the URL up?
Thanks
Ordering of rules does matter in .htaccess.
Try this code instead:
RewriteEngine On
RewriteBase /profile/
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/profile/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/?$ index.php?username=$1 [L,QSA]
Related
I am new to this.
Code from my .htaccess file goes like this:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
RewriteRule ^(.*) http://%1/$1 [R=301,NE,L]
Redirect 301 /abc/ /abcnew/
I want this to redirect from www to non-www i.e., from http://www.example.com to http://example.com
I copied:
RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
RewriteRule ^(.*) http://%1/$1 [R=301,NE,L]
this code from here Generic htaccess redirect www to non-www.
I also checked in /etc/apache2/mods-enabled folder on my linux server. There "rewrite.load" this module is present.(I think this might mean that rewrite is enabled on my server, but correct me if I am wrong.)
Redirect 301 /abc/ /abcnew/
and just FYI this above code works fine(its redirecting my old links to new links).
I also tried this.
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]
Doesn't work for me.
Please help. Thanks in advance...
Edit:
this link I found this. But not sure what should be edited. Can anyone please point out.?
You need to place external (full) redirect rules before internal rewrite ones and also make sure to use mod_rewrite rules only.
Try this:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
RewriteRule ^(.*) http://%1/$1 [R=301,NE,L]
RewriteRule ^abc/?$ /abcnew/ [L,NC,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
I got a problem with probably my .htaccess
I use the cakephp framework (2.0) and i use .htacces to redirect a non-www to a www Using this code:
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
The problem is now, when somebody clicks on the link http://mysite.nl/controller/view/E1 They go to http://www.mysite.nl/index.php
instead of http://www.mysite.nl/controller/view/E1 (with www)
And this while the rewrite works on mysite.nl to www.mysite.nl
Can somebody tell me what i do wrong? i have searched for different kind of rewrite rules but with no luck.
Full .htaccess file after comment:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>
Ordering of rules is very important in .htaccess. In general your 301s should appear before front controller rules that catch all the requests and change the request URI.
Try this code:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# add www rule
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
# cakephp front controller rule
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>
i have site, that has to rewrite site.ru and www.site.ru to www,site.ru/ru_RU.
I can't access any Apache config files. In htaccess:
Options +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^site.ru$
RewriteRule (.*) http://www.site.ru/ru_RU [QSA]
RewriteCond %{HTTP_HOST} ^www.site.ru$
RewriteRule (.*) http://www.site.ru/ru_RU [QSA]
RewriteCond %{REQUEST_URI} ^/news
RewriteRule (.*) /news [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?page=$1 [QSA]
</IfModule>
It's working in firefox or chrome, but in IE i get "this page can't be displayed". Tested on IE10 and IE8 (not compatibility view) on few computers.
If i write some junk in .htacess, i get 500 error in IE. Without .htaccess site loads ok, but i need it to rewrite url. Any ideas how to fix it?
Your flags are all wrong. Modify your rules to this:
Options +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^site\.ru$ [NC]
RewriteRule ^ http://www.site.ru/ru_RU [L,R]
RewriteCond %{HTTP_HOST} ^www\.site\.ru$ [NC]
RewriteRule !ru_RU /ru_RU [NC,L,R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?page=$1 [L,QSA]
</IfModule>
Reference: Apache mod_rewrite Introduction
I can't test it now, but here is my guess:
I think http_host ONLY contains the host part, not the uri. So you end up with an endless loop as www.site.ru is will always match again after your rewrite.
You will need another rule that checks if the uri is empty. Like this (untested):
RewriteCond %{REQUEST_URI} ^$
RewriteRule (.*) /ru_RU [QSA]
You might need another condition for the case that the uri contains a slash.
I have a shared hosting with multiple domain hosted on it. In the root folder of my hosting exists a .htaccess (say is htaccess1). Code in ht1 is:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?mydomain.com$ [NC]
RewriteCond %{REQUEST_URI} !^/mydomain/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /mydomain/$1
RewriteCond %{HTTP_HOST} ^(www.)?mydomain.com$ [NC]
RewriteRule ^(/)?$ mydomain/index.php [L]
These rules are there so that only urls with www.mydomain.com use all files of mydomain folder. I am new to url rewriting so I don't understand the meaning of what each line does. Another .htaccess (say htaccess2) file exists in mydomain folder. Code is htaccess2 is:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9]+)?/$ try.php?id=$1
RewriteRule ^([0-9]+)?$ try.php?id=$1
RewriteRule ^post/([0-9]+)?/$ post.php?id=$1
RewriteRule ^post/([0-9]+)?$ post.php?id=$1
In this file, line 6,7 works fine and redirect requests with numeric parameters to try.php but lines 8,9 doesn't work and gives a 404 page not found error, exact error string is :
The requested URL /mydomain/post/1233445 was not found on this server.
I doubt that some effect of htaccess1 is creating this problem as I tried many variations of regex in line 8,9. Please help.
Main .htaccess:
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/mydomain/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /mydomain/$1 [L]
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.com$ [NC]
RewriteRule ^$ /mydomain/index.php [L]
Changes:
Added RewriteBase at the top.
Added L flag to mark it last rule.
Added required options.
mydomain .htaccess:
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteBase /mydomain/
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteRule ^post/([0-9]+)/?$ post.php?id=$1 [L,QSA,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9]+)/?$ try.php?id=$1 [L,QSA]
Changes:
Added RewriteBase at the top.
Added L flag to mark it last rule.
Removed a redundant rule.
Moved specific rule above the generic rule.
Added required options.
I am trying to serve different urls using mod_rewrite but whatever I try it is just not working.
An example url would be
http://www.site.com/country/tours/dynamic-part/?&city=new-york,los-angeles
And I am trying to change the url using .htaccess to:
http://www.site.com/country/tours/dynamic-part/new-york,los-angeles
RewriteEngine on
RewriteBase /
RewriteCond %{QUERY_STRING} (^|&)city=([^&]*)(&|$)
RewriteRule ^country\/tours\/([a-zA-Z0-9]*)\/.+city=([^\/]*)$ http://www.site.com/country/tours/$1/$2 [L,R=301]
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
Any ideas? I though I was close but not anymore :/
The RewriteRule does NOT match the query string, see
http://httpd.apache.org/docs/current/mod/mod_rewrite.html#what_is_matched
So the .+city part of the rule will never match.
This should work tho...
RewriteCond %{QUERY_STRING} (^|&)city=([^&]*)(&|$)
RewriteRule ^country\/tours\/([a-zA-Z0-9]*)\/ http://www.site.com/country/tours/$1/%2 [L,R=301]
The subsitution can read back-referenecs to the RewriteCond pattern.