Forum Moderators: phranque

Message Too Old, No Replies

one rule for all previous urls?

         

asmith20002

1:00 pm on Sep 11, 2008 (gmt 0)

10+ Year Member



Hey guys

I am working on a site which there about 30 rewriterule in the site .htaccess file.

like :

RewriteRule ^News([0-9]*)_page([0-9]*).html files.php?name=News&id=$1&page=$2

RewriteRule ^Info([0-9]*)_page([0-9]*).html files.php?name=Info&id=$1&page=$2

I've set up wildcard subdomain lately. I'm using it for states and cities. for example :

[state1.example.com...]

I have added such line to check if subdomain is working :

RewriteCond %{REQUEST_URI} !^/index1\.php
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com
RewriteRule ^$ /index1.php?user=%1&page=home [L]

and it is working fine.

I want to know if there is someway I could add "state=%1&page=home"
to the end of every address i have. for example :

by this address :
[state1.example.com...]

it gives me :
files.php?name=News&id=100&page=2&state=state1&page=home

as I said, if it saw there's sudomain, then it adds &state=state1 to that address.

Is that possible?

Thanks in advance

p.s what about it adds &state=state1&city=city1 by state1.example.com/city1/ ?

jdMorgan

1:34 pm on Sep 11, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Not sure what you mean by your question; A list of specific requested URLs and their corresponding target filenames and queries is often most helpful.

Also a bit confused by the relationship between your "test rule" and the problem as a whole.

If you want to *keep* the query string sent by the browser, then use the [QSA] flag on RewriteRule -- See the mod_rewrite documentation. Otherwise, we're talking about a modification to your rules, but I'm not sure about the specifics.

mod_rewrite is client-request (URL) -driven. We need to know what URL is requested by the browser, and what filepath and query string you want that URL to resolve to. If you are testing and have a problem, we also need to know what filepath and query string you wanted, and what filepath and query string you actually got.

Jim

asmith20002

8:42 pm on Sep 11, 2008 (gmt 0)

10+ Year Member



first thanks for replaying.

The site has about 30 rewrite rule. it is making php urls, to html friendly urls.

Now i've added subdomains so I could have 2 differend addresses for each previous url , for example :

http://www.example.com/news123.html
[state1.example.com...]

I want to get from the first this :
http://www.example.com/file.php?name=News&id=123

and this one from the second :
http://www.example.com/file.php?name=News&id=123&state=state1

As you see te difference is that the seoncd one has "state=state1" more.

Now that I have like 30 kind of this rewrite rule, I was wondering if I could add one line, so that it cover all 30 rewrite rules.

a rule like : If there was a subdomain, then add "state=state1".

Sorry If i wasn't clear enough.

jdMorgan

9:15 pm on Sep 11, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You have a larger problem if you have more than one URL for the same page or 'item' on your site. This is duplicate content. It confuses the search engines, and they will pick one URL and ignore the other -- Which one they pick will not be your choice.

If you generate enough duplicate content, you may invoke a filter. If you generate a ton of duplicate content, you may invoke a penalty.

As far as what I see here, you need two rules: One to handle the case with a "state" subdomain, and the other to handle URLs without the state subdomain. Aside from the state/no-state distinction, you only need one rule, not 30. Everything else can be done in a single rule with back-references to copy the appropriate subdomain and URL-path-parts into the two or three query string variables. See mod_rewrite RewriteRule "back-references" documentation at Apache.org.

Jim

asmith20002

8:25 am on Sep 12, 2008 (gmt 0)

10+ Year Member



1. How you think I may have duplicate content?
2. Could you please help me with some examples?

For something like this : state1.example.com/city1/

I wrote :

RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com
RewriteRule ^$ index.php?state=%1
RewriteRule ^.*/$ index.php?state=%1&city=$1 [L]

the state1.example.com is working fine. but state1.example.com/city1/

is not working.

jdMorgan

4:09 pm on Sep 12, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Your second RewriteRule pattern was incorrect and did not create the $1 back-reference because the parentheses were missing. Also RewriteRules cannot "share" RewriteConds; RewriteConds apply only to the single RewriteRule that follows them. Your two rules should look something like this:

RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com
RewriteRule ^$ index.php?state=%1 [L]
#
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com
RewriteRule ^([^/]+)/$ index.php?state=%1&city=$1 [L]

Jim

asmith20002

8:42 pm on Sep 12, 2008 (gmt 0)

10+ Year Member



really thanks.

is there anyway I could check how my code work in .htaccess?

atm I do te changes, upload it on the server and test on the site. If the code is wrong it mess up the site and i have to change it back.

since I have about 500,000 views per month, I don't wanna "test" it till i get the desire result.

p.s How many rewrite rules in htaccess is normal ? I mean won't slow down the site ...

jdMorgan

7:25 pm on Sep 13, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sure! Put an extra RewriteCond on each new rule to test for your own IP address in REMOTE_ADDR. That way, the new rule is only executed for *you* and no-one else will see its effects. Example:

RewriteCond %{REMOTE_ADDR} ^192\.168\.10\.12$

Where "192.168.10.12" is your local computer's WAN address (assigned by your ISP), or a partial address if you're on DHCP and you don't always get assigned the exact same WAN address. For example:
 RewriteCond %{REMOTE_ADDR} ^192\.168\.10\. 

would match any address from 192.168.10.0 through 192.168.10.255

Leave those extra RewriteConds in place until you've thoroughly tested everything, then remove them when finished with testing.

Many Apache developers download Apache or one of the free WAMP, XAMP packages and install it on an old PC for local testing. Apache will run quite nicely on a Windows 95-generation PC; It does not take a lot of CPU power to run Apache -- It's quite small and efficient by modern standards.

Jim

asmith20002

7:42 pm on Sep 13, 2008 (gmt 0)

10+ Year Member



I have setup local enviroment with apache, php and mysql. but my .htaccess dooes not work local.

Thanks for the hint about the ip.