View Full Version : Mod rewrite help
anil34
02-27-2005, 10:20 AM
Hi
I am sure this question has been addressed, but I did not have much luck with it. I am trying to create a site similar to craigslist. Craigslist.org is a separate site that I found on internet. I am trying to start a site similar to that site. So I want to do what that person has done as far as mod-rewrite goes.
(substituted craigslist for 'mydomain')
washingtondc.mydomain.org/
austin.mydomain.org/stp
boston.mydomain.org/stp/61073929.html
obviously, these are just examples, the 'washingtondc', 'stp', '6107929' could be anything.
So if my site is mydomain.com, I would like to access by
mydomain.com/city.php?city=washingtondc
mydomain.com/categ.php?city=austin&categ=stp
mydomain.com/ad.php?city=boston&categ=stp&ad=61073929
Could someone please suggest the mod rewrite rules.
I think I need to mention that this domain is a sudomain to my main domain, domain.com meaning that it is the same url as mydomain.domain.com. The hosting provider allows subdomain
if i have have /home/username/pubic_html/mydomain.
Everyone will be accessing it via mydomain.com or www.mydomain.com. I am assuming that I will have to put .htaccess file in my subdomain root directory.
Thanks for everybody's time.
Anil
seomike
02-28-2005, 01:27 PM
Here are the rewrite rules and conditions that should get you started :)
# start htaccess file
RewriteEngine On
RewriteBase /
# subdomain rewrite
RewriteCond %{HTTP_HOST} (.*)\.mydomain\.com$
RewriteCond %{REQUEST_URI} !-f
RewriteCond %{REQUEST_URI} !-d
RewriteRule ^$ /city.php?=%1 [L]
#subdomain with directory
RewriteCond %{HTTP_HOST} (.*)\.mydomain\.com$
RewriteCond %{REQUEST_URI} /[^/]+/?$
RewriteRule ^([^/]+)/?$ /categ.php?city=%1&categ=$1 [L]
#subdomain with directory and file
RewriteCond %{HTTP_HOST} (.*)\.mydomain\.com$
RewriteCond %{REQUEST_URI} /[^/]+/.*\.html$
RewriteRule ^([^/]+)/([^/]+)\.html$ /ad.php?city=%1&categ=$1&ad=$2 [L]
#end htaccess file
This should work for all your exmples. All you have to do now is test it :)
anil34
02-28-2005, 03:53 PM
Mike, thanks for your time and help. This looked promising but it is not working
I put the files city.php, categ.php, ad.php on my server which can be accessed by going to this site chehra.com (without www). You can go to the site
1. Anything with www gives "internal server" error
2. I do not think mod rewrites are working as
cityname.chehra.com - cannot be displayed
cityname.chehra.com/categ - cannot be displayed
cityname.chehra.com/categ/12.html - cannot be displayed
I put your htaccess file in the root of this directory, first I thought it is due to subdomain, but I tried on the root domain as well, get the same errors.
Your help is appreciated.
Thanks
Anil
Connie
02-28-2005, 04:54 PM
Try adding Options +FollowSymLinks above the RewriteEngine on. SymLinks is required when using mod_rewrite. On some servers if is on at the server level. If not you have to add it to the rewrite rule.
anil34
02-28-2005, 05:27 PM
Hi, thanks, I tried before (RewriteEngine On) as well, but not working (same errors)here is my file
RewriteEngine On
Options +FollowSymlinks
RewriteBase /
# subdomain rewrite
RewriteCond %{HTTP_HOST} (.*)\.chehra\.com$
RewriteCond %{REQUEST_URI} !-f
RewriteCond %{REQUEST_URI} !-d
RewriteRule ^$ /city.php?=%1 [L]
#subdomain with directory
RewriteCond %{HTTP_HOST} (.*)\.chehra\.com$
RewriteCond %{REQUEST_URI} /[^/]+/?$
RewriteRule ^([^/]+)/?$ /categ.php?city=%1&categ=$1 [L]
#subdomain with directory and file
RewriteCond %{HTTP_HOST} (.*)\.chehra\.com$
RewriteCond %{REQUEST_URI} /[^/]+/.*\.html$
RewriteRule ^([^/]+)/([^/]+)\.html$ /ad.php?city=%1&categ=$1&ad=$2 [L]
#end htaccess file
Connie
02-28-2005, 08:31 PM
Try adding this.
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain\.com
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=permanent,L]
then add Mikes rewrite rules.
What the above does is redirect all request for domain.com to www.domain.com and you need this anyway. Does the rewrite Mike posted work with domain.com?
I don't know a lot about mod_rewrite, but have learned enough for my simple needs the hard way. In looking at what Mike provided it looks to me like it might only work with domain.com but I'm not 100% sure.
Couple of other comments. I would start out with this in the root folder. This should take care of any rewrite conditions that apply to sub folders.
Make sure when you are uploading the .htaccess that it is uploading as text an not binary.
Hopefully Mike or someone else who is a lot more knowledgeable than me will get involved to help you out.
seomike
02-28-2005, 11:58 PM
I think it's the first condition and rule that is bombing if you are getting a 500 server error.
I was trying to detect if only the subdomain was being requested. hmmm ok let's try this set of rules instead. I had to do a bit of research so I think this will work since I normally don't use subdomains.
# start htaccess file
RewriteEngine On
Options +FollowSymlinks
RewriteBase /
# rule 1 subdomain rewrite
RewriteCond %{HTTP_HOST} !^(www\.)?([^.]+)\.mydomain\.com/?[^/]+$
RewriteRule ^$ /city.php?=%2 [L]
# rule 2 subdomain with directory
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.mydomain\.com.*$
RewriteCond %{REQUEST_URI} /[^/]+/?$
RewriteRule ^([^/]+)/?$ /categ.php?city=%2&categ=$1 [L]
# rule 3 subdomain with directory and file
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.mydomain\.com.*$
RewriteCond %{REQUEST_URI} /[^/]+/.*\.html$
RewriteRule ^([^/]+)/([^/]+)\.html$ /ad.php?city=%2&categ=$1&ad=$2 [L]
#end htaccess file
You wont need Connie's addition since this version will detect if www. is there or not infront of the subdomain.
Definition
Rule 1 = detect the full domain name being called. If it has a www. or not copy the 2nd back reference or info inside the (). If there are no folder or any characters after the .com besides a / then it is a root request so use condition 1.
Rule 2 = detect the full domain name being called. If it has a www. or not copy the 2nd back reference or info inside the (). If there is a sub folder only and no file then use this rule
Rule 3 = detect the full domain name being called. If it has a www. or not copy the 2nd back reference or info inside the (). If there is a sub folder and a file ending in .html then use this rule.
anil34
03-03-2005, 08:16 AM
Mike, thanks for your help. Here is my file,
-------
# start htaccess file
RewriteEngine On
Options +FollowSymlinks
RewriteBase /
# rule 1 subdomain rewrite
RewriteCond %{HTTP_HOST} !^(www\.)?([^.]+)\.chehra\.com/?[^/]+$
RewriteRule ^$ /city.php?=%2 [L]
# rule 2 subdomain with directory
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.chehra\.com.*$
RewriteCond %{REQUEST_URI} /[^/]+/?$
RewriteRule ^([^/]+)/?$ /categ.php?city=%2&categ=$1 [L]
# rule 3 subdomain with directory and file
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.chehra\.com.*$
RewriteCond %{REQUEST_URI} /[^/]+/.*\.html$
RewriteRule ^([^/]+)/([^/]+)\.html$ /ad.php?city=%2&categ=$1&ad=$2 [L]
#end htaccess file
--
Andy here my observations (actually you can go the the URL and test it, I might have missed something)
0. chehra.com goes to city.php
1. Anything with www.chehra.com bombs (500), I noticed that you have www\. and this dot is taken care of before \.chehra as well. so tried removing one dot from www but same thing.
2. dc.chehra.com - page cannot be displayed
3. www.chehra.com/categ - 500
4. chehra.com/categ - 404 (but works fine if I have a valid subdiretory)
eg. chehra.com/pics works, www.chehra.com/pics does not (500)
5. www.chehra.com/categ/123.html (500)
6. chehra.com/categ/123.html (404) coincides with #4 above
7. dc.chehra.com/categ/123.html (Page cannot be displayed)
8. dc.chehra.com/categ (Page cannot be displayed)
9. dc.chehra.com/pics (Page cannot be displayed)
10. chehra.com/ad.php, chehra.com/city.php, chehra.com/categ.php work fine but without www.
I tried the same file on my main server to see if it is because of subdomain, but same results
Thanks again for your interest
Anil
seomike
03-13-2005, 03:19 PM
500 error is because the syntax is wrong in my code :o
# start htaccess file
RewriteEngine On
Options +FollowSymlinks
RewriteBase /
# rule 1 subdomain rewrite
RewriteCond %{HTTP_HOST} !^(www\.)?([^.]+).?chehra\.com.*$
RewriteRule ^$ /city.php?=%2 [L]
# rule 2 subdomain with directory
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+).?chehra\.com.*$
RewriteCond %{REQUEST_URI} /[^/]+/?$
RewriteRule ^([^/]+)/?$ /categ.php?city=%2&categ=$1 [L]
# rule 3 subdomain with directory and file
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+).?chehra\.com.*$
RewriteCond %{REQUEST_URI} /[^/]+/.*\.html$
RewriteRule ^([^/]+)/([^/]+)\.html$ /ad.php?city=%2&categ=$1&ad=$2 [L]
#end htaccess file
Try this and see if it work. I've made a few changes to the syntax.