PDA

View Full Version : Mod rewrite tips and tricks


seomike
01-27-2005, 01:47 AM
Tip 1
instant 301 from old dynamic url to new static url
(will only work if you use the exact variables from the dynamic url)

Old dynamic url:
www.somesite.com/catalog.php?cat=widgets&product_id=1234New static url:
www.somesite.com/catalog/widgets-1234.html#start .htaccess code
RewriteEngine On
RewriteBase /

RewriteCond %{QUERY_STRING} ^cat\=([^&]+)\&product_id\=([^&]+)$
RewriteRule ^$ /catalog/%1-%2.html [R=301,L]Reg Expression notes:
[^&]+ mean find any character except the "&" since it is what seperates the variables in a string. you can back reference matches in a rewrite condition using ()'s just like your rewrite rules but to call them you have to use a % instead of a $.

Benefits:
1. You don't have to hand write 1,000's of 301 redirects

2. Spiders can easily pick up the 301 and pass the scores and index the new urls much faster than having to crawl the entire site over from scratch.

3. Users clicking old dynamic urls in the SERPS will not get a 404 error. They'll go straight to the new static urls.

Tip 2

Change a product name or change a mispelling and you've just lost all page scores to the static mod rewritten url. Unless...

Example url:
www.somesite.com/dinnerplates/cassa-stone/ (http://www.somesite.com/dinnerplates/cassa-stone/)
Should be spelled
www.somesite.com/dinnerplates/casa-stone/ (http://www.somesite.com/dinnerplates/casa-stone/)

RewriteEngine On
RewriteBase /

# detect if the file being requested is a mispelling
RewriteCond %{REQUEST_URI} /.*cassa.*$
#redirect to the correct spelling using a 301
RewriteRule ^(.*)cassa(.*)$ /$1casa$2 [R=301,L]Reg Expression notes:
.* means match any character 0 to infinite times. so .*cassa.* so zzzzcassazzzz would be a match as well as /cassa-stone/. All I'm doing is back referencing everything before and after cassa and then copying it into the new url and doing a redirect.

Benefits
This can really save your @$$ nuff said. :D

Tip 3

Make the unormalized, normalized for Yahoo!'s sake.

Example code:
<a href="some-directory/">some directory</a>

All bots and SE's are different. ugh. Google is very good at indexing and scoring exactly what is in the <a href> tag whether the url is normalized or not. Yahoo on the other hand is a stickler for normalization and will index the example above like so in there SERPS http://www.somesite.com/some-directory

to keep a 404 from kicking in if some-directory needs to be picked up by your mod rewrite make sure you end all your RewriteRules that end their mathcing on directory/folder like so.

RewriteRule ^([^/]+)/?$ /somepage.php?name=$1 [L]who thought a question mark would ever be an answer :D
? mean match the previous character 0 to 1 times. and will fix any normalization problems you might have encountered with Y!.


#This will send you into a horrible loop and bad thing may happen :(
#so don't use this
RewriteRule ^(.*)$ /somepage.php?name=$1 [L]Benefits
1. users will not hit a 404 when clicking a yahoo SERP :)

2. worth its weight in gold!

3. Your site will not look like a bad cloak hehe

The Definitive Guide to Apache mod_rewrite (http://www.amazon.com/gp/product/1590595610?ie=UTF8&tag=buypokboo-20&linkCode=as2&camp=1789&creative=9325&creativeASIN=1590595610)

Robert_Charlton
01-27-2005, 03:29 AM
seomike - Great post... Thanks.

The code for your "old dynamic url" gets compressed by the bbs into a shortened version that's hard to read. Here it is as code....

http://www.somesite.com/catalog.php?cat=widgets&product_id=1234

I find regular expressions really hard, particularly since I don't use them that often, but I've found I can understand what's going on if they're explained line by line.

Are you game for that? It's a lot of work, I know, and may be beyond the scope of what you intended to do.

seomike
01-27-2005, 06:01 AM
Are you game for that?

I added some notes to the post above ;)

hooter
02-10-2005, 08:48 PM
First Mike,

Excellent tutorial - problem is I'm as dense as a brick when it come to deciphering what \$* goes in where or comes out there to accomplish what I'm after. Here is an example of what my old dynamic URLs in SERPS look like:
product.php?productid=60&cat=1&page=1
home.php?cat=12

With my mod_rewrite rules and altering my application code I can now have static URLs like:
ProductName-pr-60.html
CategoryName-c-12.html
where pr is product id and c is category number. Using these as an example, how would I do what you did to 301 redirect those in that dynamic format into the static example format?

I realize I don't have the Product or Category name embedded in the original dynamic URLs but if I could just redirect to something generic that would still work I think?
example: Product-pr-60.html or Category-c-12.html

Here are my rewrite rules which might help understand what redirect patterns would/might work:
RewriteRule ^(.*)-p-([0-9]*)-pr-([0-9]*)(.*).html$ product.php?page=$2&productid=$3&$4
RewriteRule ^(.*)-pr-([0-9]*)(.*).html$ product.php?productid=$2&$3
RewriteRule ^(.*)-c-([0-9]*)-p-([0-9]*).html$ home.php?cat=$2&page=$3
RewriteRule ^(.*)-page-([0-9]*).html$ pages.php?pageid=$2
RewriteRule ^(.*)-c-([0-9]*).html$ home.php?cat=$2

Or would it just be safer to redirect all of these dynamic to my default home.php (all my new rewrite static URLS can be followed from there)

Many thanks in advance.

nex99
03-25-2005, 02:05 AM
Dear Mike,

At first, Thanks for your brilliant post.

i wants to use mod_redirect rule to change :

http://www.somesite.com/v_b2b.php?category=sale&start=50
To
http://www.somesite.com/v_b2b/sale-50.html

Code Used :

#start .htaccess code
RewriteEngine On
RewriteBase /

RewriteCond %{QUERY_STRING} ^category\=([^&]+)\&start\=([^&]+)$
RewriteRule ^$ /v_b2b/%1-%2.html [R=301,L]


i use the exact code in my .htaccess but seems change nothing. is their anything wrong?

seomike
03-25-2005, 12:21 PM
Hey Nex99 welcome to the forums.

Change your code in the pages to out put a static url. Mod rewrite just translates the static back its dynamic form.

Then all you need is this rule

#start .htaccess code
RewriteEngine On
RewriteBase /

RewriteRule ^v_b2b/([^/]+)-([^/]+)\.htm$ /v_b2b.php?category=$1&start=$2 [L]

pintofmilk
04-15-2005, 07:07 AM
Hi Mike,

Great article... using it I have finally conquered the mod_rewrite problem that I has been on my todo list forever!

As well implementing mod_rewrite to translate static HTML URLs to dynamic PHP ones, I want to eliminate the dynamic URLs from the search engines. I cannot quite get it working though!

This is my working static to dynamic rule:
RewriteRule ^r/accom_one/(.*)\.html /r/accom_one.php?a=$1

And this is what I am trying to get working to eliminate the dynamic URLs from the search engines:
RewriteCond %{QUERY_STRING} ^a=([0-9]+)$
RewriteRule ^r/accom_one\.php /r/accom_one/%1.html [R=301,L]

When I put the second bit in the file with the first bit, all the accom_one pages fail - static and dynamic. Have I created a mod_rewrite loop or just made a simple mistake?

Thanks,
Ben

seomike
04-15-2005, 01:13 PM
I'm glad that the tips helped you out! It's great to hear.

As per your rewrite rule try this :)


RewriteCond %{QUERY_STRING} ^a=([^&]+)$
RewriteRule ^.*$ /r/accom_one/%1.html [R=301,L]

pintofmilk
04-15-2005, 01:23 PM
Thanks Mike,

But won't that rule try to match any URL where the variable 'a' has been set? I only want it to operate on accom_one.php pages and not any others...?

Thanks,
Ben

alfo
04-15-2005, 11:46 PM
Hi all,

Firstly this is just what I have been looking for, this is the best instructions I have found, thanks!

I am afraid I am on the first step to try and understand this so please be patient with me. Just a few simple questions I am sure to most here would know the answers to.

1) I wish to try and change the url http://www.mysite.com/proddetail.php?prod=PRODUCT-1 to http://www.mysite.com/PRODUCT-1.html
I have hacked and chopped but can't get the new static page to show in the browser.

2) If I create a sitemap with the new static links would that be enough or would every link on the site need to point to the new static pages?

3) Do I need to do anything to avoid SE finding double content .php/.html?

Sorry if these questions all seem a bit simplistic.

Thanks so much for a fantastic posting.
alfo

seomike
04-16-2005, 02:37 AM
But won't that rule try to match any URL where the variable 'a' has been set? I only want it to operate on accom_one.php pages and not any others...?

RewriteCond %{REQUEST_URI} ^.*accom_one\.php
RewriteCond %{QUERY_STRING} ^a=([^&]+)$
RewriteRule ^.*$ /r/accom_one/%1.html [R=301,L]

Pintofmilk this extra condition will limit it to the accom_one.php file ;) Good luck!

1) I wish to try and change the url http://www.mysite.com/proddetail.php?prod=PRODUCT-1 to http://www.mysite.com/PRODUCT-1.html
I have hacked and chopped but can't get the new static page to show in the browser.

What you need to look for is the echo or print functions that out put the variables in the coding. something like this

echo '<a href="somefile.php?var='.$db_val['column_name'].'">...</a>';

If you can locate where the links are generated in the code then you can just change the structure in the href="" to make it out put a static looking url.

If you can do that then you just need to add this to your .htaccess file in the root of your website on the server


#htaccess mod rewrite code
RewriteEngine On
RewriteBase /

#code to tell spiders where your new page is
RewriteCond %{QUERY_STRING} ^prod=([^&]+)$
RewriteRule ^.*$ /%.html [R=301,L]

#code to write product-1.html back to proddetail.php?... on the server
RewriteRule ^([^/]+)\.html$ /proddetail.php?prod=$1 [L]



2) If I create a sitemap with the new static links would that be enough or would every link on the site need to point to the new static pages?

It would actually be enough, but you'd have duplicate pages. If you can pull off the code change to output a static url, plus a site map and then 301 all versions of the old dynamic urls to the static ones you'll be cooking with some serious gas! :D

3) Do I need to do anything to avoid SE finding double content .php/.html?

Yes, use Tip 1 that will tell the spider that the old dynamic url is now permantly changed to the new static one (I wrote it in the htaccess code above also)

Good luck

alfo
04-16-2005, 06:19 AM
Hi Mike

Thank you so much for your help! :) This is what I have found so far

$startlink='<a href="proddetail.php?prod=' . urlencode($rs["pId"]) . (@$catid != "" && @$catid != "0" && $catid != $rs["pSection"] ? '&cat=' . $catid : "") . '">';

I have tried the following, the link looks good in the browser i.e. http://www.mysite.com/PRODUCT-1.html but the page never loads (the browser doesn’t timeout either).

$startlink='<a href="' . urlencode($rs["pId"]) . (@$catid != "" && @$catid != "0" && $catid != $rs["pSection"] ? '&cat=' . $catid : "") . '.html">';

Do you know what I might be doing wrong?

Many thanks for your time,
alfo

madgett
05-04-2005, 06:47 AM
How could I accomplish this on a redirect request from .htaccess:

http://www.server1.com -> http://12.23.34.45/dir/dir/dir

But it should still display in the address bar: http://www.server1.com

Anybody have any ideas?

fazman
06-18-2005, 10:03 PM
Hi Guys, Im new to this forum. Just been reading over this thread and it seems Ive come to the right place.

Im looking for a way to link to say

http://www.domain.com/order/PH6 and have the PH6 become a variable to be used in a php script.

I am pretty sure I can do it with the mod rewrite rule in htaccess to change it from

http://www.domain.com/order/PH6 to http://www.domain.com/order/?order=PH6

If I can do this with the modrewrite rules then I can break it back down and let my script do the rest.

Ive noticed that everyones question have sort of been the opposite way to what Im after. Changing the variable to a static url, where I want to change the static url to a variable.

Does anyone have any thoughts on how to do this.

Thanx

Fazman

phpmaven
06-18-2005, 10:19 PM
You will need to change your approach slightly to avoid having having Apache go into an infinite loop, but something like the following should work.

RewriteEngine on
RewriteBase /

RewriteRule ^order/(.*) /order.php?order=$1 [L]

fazman
06-18-2005, 11:05 PM
You will need to change your approach slightly to avoid having having Apache go into an infinite loop, but something like the following should work.

RewriteEngine on
RewriteBase /

RewriteRule ^order/(.*) /order.php?order=$1 [L]


Hi phpmaven. thanx for the fast reply. I tested that out. Im still getting a 404 error when i try it out though.

If you would like to see what I mean, it should go from

<snip>

the second url would then change the header location to where that variable tells it to.

Hopefully the urls are not offensive.

Also is there anything else I need in my htaccess file other than this

RewriteEngine on
RewriteBase /

RewriteRule ^order/(.*) /index.php?order=$1 [L]

Thanx

Fazman

seomike
06-20-2005, 01:08 AM
Here is the best way to do it. Using the /?$ will catch the url with or without the trailing slash :)

RewriteEngine on
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQEUST_URI} ^/order.*
RewriteRule ^order/([^/]+)/?$ order/?order=$1 [L]

fazman
06-21-2005, 10:07 PM
Hi Mike, thanx for that. I tested it out in both the root directory and the order/ directory with the .htaccess files. Neither worked. It still seems to assume that the order/PH6 is another directory, when its supposed to work out the PH6 is actually a variable.

I know that the mod rewrite rule works on my server as I use it on some other domains I needed it on. This one is just frustrating me. I only wanted to do it this way for those search engines out there that dont follow variables that use the ?.

What you have given me makes sense to me, and looks like it should do it, but for some reason I just keep coming up with a 404 page from it. I even tried to change the

RewriteEngine on
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQEUST_URI} ^/order.*
RewriteRule ^order/([^/]+)/?$ order/?order=$1 [L]

TO

RewriteEngine on
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQEUST_URI} ^/order.*
RewriteRule ^order/([^/]+)/?$ order/index.php?order=$1 [L]

to see if adding the actual filename in would make a differance. It didnt.

It just needs to go from domain.com/order/PH6 to domain.com/order/index.php?order=PH6

Anymore thoughts would be greatly appreciated

Thanx

faz

seomike
06-21-2005, 10:25 PM
Try this

RewriteEngine On
RerwiteBase order/
RewriteCond %{REQUEST_URI} ^/order.*
RewriteRule ^order/([^/]+)/?$ index.php?order=$1 [L]


if that doesn't work

strip of the conditions and just use the rule with the full url filled out.

#test to see if the matching is even working at all
RewriteEngine On
RewriteRule ^order/(PHP6)/?$ index.php?order=$1 [L]

if that doesn't work then there is something up with the modrewrite configurations... :eek:

fazman
06-22-2005, 12:02 AM
Thanx again Mike.

I got the first one uploaded, had to fix a spelling mistake. It returned me a 500 server error. Just checked the error log and it says

.htaccess: RewriteBase: argument is not a valid URL

The second one came up with a url not found

Getting closer now though I think, especially since I got a 500 error. Its something differant :)

faz

fazman
06-22-2005, 01:51 AM
Hi Mike, I actually got to chat with my host and he got it all fixed up for me.

He commented out the ones that werent working so we could see the differance.

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /order/
#RewriteCond %{REQUEST_URI} ^/order.*
#RewriteRule ^order/([^/]+)/?$ index.php?order=$1 [L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^order/([A-Za-z0-9]*)$ /order/index.php?order=$1 [QSA]

</IfModule>

Its all working smoothly now.

Thanx again for your help, and hopefully this will help someone else in the future.

faz

seomike
06-22-2005, 11:03 AM
Instead of this rule
RewriteRule ^order/([A-Za-z0-9]*)$ /order/index.php?order=$1 [QSA]


I would suggest using this one it will be a faster and less strain on the server. it will also pick up the url with or with out the trailing /

RewriteRule ^order/([^/]+)/?$ /order/index.php?order=$1 [QSA,L]


When you do a /([A-Za-z0-9]*)$ you are looking for alpha numeric 0 to infinite times without a trailing / this is fine but if anyone links to the page with a trailing / it won't get picked up by the mod.

:)

perfectlover
12-09-2005, 04:49 PM
Hi,
Based on your tutorial, I have modified code for my site. I intend to change from
http://www.mydomain.com/cgi-bin/books/displayitem.cgi?asin=0815332181

to

http://www.mydomain.com/cgi-bin/books/displayitem/0815332181.htm

my code is as follows:
RewriteEngine On
RewriteBase /

RewriteCond %{QUERY_STRING} ^asin\=([^&]+)$
RewriteRule ^$ /displayitem/%1.html [R=301,L]


this code did not do anything. Please help me out.

Thank you,

Stefan

seomike
12-09-2005, 05:33 PM
You need to change your code to output the static url. Then use a rewrite rule to rewrite static -> dyamic on the server.

http://www.mydomain.com/cgi-bin/books/displayitem/0815332181.htm ->
http://www.mydomain.com/cgi-bin/books/displayitem.cgi?asin=0815332181


RewriteEngine On
RewriteBase /

RewriteRule ^cgi-bin/books/displayitem/([^/]+)\.htm$ cgi-bin/books/displayitem.cgi?asin=$1 [L]

Just add this to the .htaccess on the root of your website.

newline100
12-10-2005, 11:59 PM
Hi,

I appreciate your help on these boards but I am new to this stuff & having some problems. Let me start with the basics:

I have successfully used the mod_rewrite by changing:

http://www.mysite.com/mydirectory/postlist.php?Cat=0&Board=myfile
to
http://www.mysite.com/mydirectory/myfile.htm
by using the following .htaccess code:

RewriteEngine on
RewriteBase /mydirectory/
RewriteRule ^myfile\.htm$ postlist.php?Cat=0&Board=myfile [T=application/x-httpd-php]

Here is my problem, I have to physically type in the static address to make sure this code works. On my webpage, when I run my mouse pointer over the target link, the dynamic link shows up on the bottom left hand corner of the browser. Furthermore, when I click the link, it still defaults to the old dynamic url.
Is there extra code that will make the static URL show up (redirect) in the browser window? Also, is there a way to make the new (static) URL show up on the bottom left hand corner of my browser before it is clicked? Will that even matter to the search engine spiders?

If you could please explain all of this very carefully, I would appreciate it. As I stated, I'm new to all of this.

seomike
12-12-2005, 01:15 PM
Mod rewrite doesn't rewrite your links. It rewrites urls that your links output back to the dynamic form on the server. see this (http://www.webforgers.net/mod-rewrite/) for more info on mod rewrite's can and cannot's.

So you have to open your page code and change how it outputs your <a href> Once you do that then you make a rewrite rule to rewrite the static to dynamic :)

neo4242002
12-28-2005, 05:21 AM
Hello...

interesting thread... I have something to do.. when I maintenance my site I put under construction page
something like mydommain.com/index.htm

and i want to redirect al traffics come to this site to that page..

something like

if some one try to visit mydommain.com/see.php or mydommain.com/stuffs/index.htm or what every the URL they all forward to my Under construction page.

Dose anyone can help me please?

Cascade
02-06-2006, 03:59 PM
Hi all,

Just found the forums. They are great, I have a lot of reading to do :)

I currently have this url:

http://www.domain/archives/category/ely-history/

But ideally I would like to do this:

http://www.domain.com/ely-history/

Can I do this?

Many thanks

grnidone
02-06-2006, 04:48 PM
RewriteRule ^$ /catalog/%1-%2.html [R=301,L]

^ -- beginning
$ -- ending
/catalog/ -- anything with the letters 'catalog' in that order only
%1 -- variable one
%2 -- variable two
.html -- put period and letters 'html' in that order only

And then... I need help. Does the R stand for 'Redirect' and 301 that it is a 301 redirect? What is 'L'? And tell me what the [ ] at the end is called so I can look it up and learn more. (Extension?)

Thanks.

seomike
02-06-2006, 10:43 PM
[] is a flag holder

R, by default is a 302 redirect
R=301, tells the server to issue a 301 header instead of 302
L, mean this is the last rule.
F, Forbidden
QSA, query string append if there is any query string on the url add it to the rewrite rules output.

These are the main ones you'll use I have a list of what all the flags mean here

http://www.webforgers.net/mod-rewrite/mod-rewrite-syntax.php

finch8243
02-24-2006, 09:49 PM
seomike or anyone else -- here's a tough one I've been struggling with:

One of my categories is music -- you go to the music page and there are a ton of items. I currently have my mod rewrite changing this:
www.mysite.com/shop/music

to this:
www.mysite.com/index.php?category=music

so my mod rewrite language is something like:
RewriteRule ^shop/music$ index.php?category=music [NC]

Easy enough. But on the music category page, I want to have some "sort by" links -- sort all music by price, sort by bestsellers, etc. I was thinking of having the links as follows:

www.mysite.com/shop/music?sortby=price
www.mysite.com/shop/music?sortby=bestsellers
etc etc

I'd like it to load a page:
www.mysite.com/index.php?category=music&sortby=price
www.mysite.com/index.php?category=music&sortby=bestsellers

So what would the mod rewrite language be? I'm having trouble telling it to "look after the question mark in the requested URI and add the following stuff on to the underlying page".

Any way to do this? Incidentally, I'd like to avoid doing this:
www.mysite.com/shop/music/price
www.mysite.com/shop/music/bestsellers

I know how to do this already. Why do I want to avoid this? Because my analytics solution reads each of those as separate pages and I don't want it to do that.

Thoughts? Thanks for any help fellas.

futureweb
02-25-2006, 07:34 AM
Do I have this right ?

The Plan

from this:
mobiles.php?net=T-Mobile&make=Motorola&model=V3&id=9

to this:
/mobiles/T-Mobile/Motorola/V3-9.html



RewriteCond %{QUERY_STRING} ^net\=([^&]+)\&make\=([^&]+)\&model\=([^&]+)\&id\=([^&]+)$
RewriteRule ^$ /mobiles/%1/%2/%3-%4.html [R=301,L]


Thanks in advance.

futureweb
02-25-2006, 07:05 PM
here is what I have managed to figure out.


#start .htaccess code

RewriteEngine On
RewriteRule ^mobiles/(.*)/(.*)/(.*)-(.*).html$ mobiles.php?net=$1&make=$2&model=$3&id=$4 [L]
RewriteRule ^makes/(.*)/index.html$ make.php?make=$1 [L]
RewriteRule ^networks/(.*)/index.html$ network.php?net=$1 [L]


lets take the first one as an example

RewriteRule ^mobiles/(.*)/(.*)/(.*)-(.*).html$ mobiles.php?net=$1&make=$2&model=$3&id=$4 [L]


the dynamic url: mobiles.php?net=Orange&make=motorola&model=V3&ID=4

^mobiles/ = the name of the script mobiles.php
/(.*) = catches the name of the dir so net=motorola will be motorola and then the next /(.*) will pick up make=Orange making orange and /(.*) will pick up model=V3 as V3 and -(.*) will pick up the ID=4 as -4 .html is the file ext

so my url can be
mobiles/Orange/motorola/V3-4.html

and the above rule will translate it into:
mobiles.php?net=Orange&make=motorola&model=V3&ID=4

seems to work well have I done it right is there a more efficent way of doing it ?

jphilapy
02-27-2006, 01:48 PM
Hi,

In the past we used mod_rewrite to rewrite the urls of all the html pages so they pointed to a php page. Now when google access our server to check the sitemap page they gave us it says we are returning a 200 when we should be returning a 404. I'm not sure if they are testing the Sitemap page only and coming to their conclusion or if they are checking for an actual bogus page on our server and getting a 200 error when they expect a 404.

If they are only checking their assigned error then I know a workaround. That is, have the server rewrite check for their page name, and then dont process it in rewrite. If they are checking for a bogus page, than i don't know yet how to resolve this. If this is the case than I would appreciate some suggestions.

Thanks,
Jeff

seomike
02-27-2006, 07:30 PM
I don't think I understand the question fully. Are you having an issue of Google still crawling old versions of the url because the site map still links to it?

or there was an old site map and it's not there anymore but when called the mod rewrite picks it up and sends 200 ok response and you want to kill that?

jphilapy
02-28-2006, 05:12 AM
I don't think I understand the question fully. Are you having an issue of Google still crawling old versions of the url because the site map still links to it?

or there was an old site map and it's not there anymore but when called the mod rewrite picks it up and sends 200 ok response and you want to kill that?

The issue was I am trying to start using the sitemap service that google offers, however the returned this error message when I submitted the site:

=====
We've detected that your 404 (file not found) error page returns a
status of 200 (OK) in the header.
This configuration presents a security risk for site verification and
therefore, we can't verify your site. If your web server is
configured to return a status of 200 in the header of 404 pages, and
we enabled you to verify your site with this configuration, others
would be able to take advantage of this and verify your site as well.
This would allow others to see your site statistics. To ensure that
no one can take advantage of this configuration to view statistics to
sites they don't own, we only verify sites that return a status of
404 in the header of 404 pages.
=====

The error message is confusing as I am not sure how they get a 404 error. I am using server rewrites, however when they visit the page that they gave us they send us back that error message. Sorry it doesnt make any sense to me.

However I did solve the problem, I disabled the server rewrites long enough to resubmit the site to the google service. They approved it and then I renabled the server rewrite.

My bottom line question is, how are they coming to the above conclusion? In my situation what would I be doing for them to conclude the above?

Jeff

seomike
02-28-2006, 11:19 AM
You got me. lol I've never heard anything like this :D

BradBristol
02-28-2006, 11:44 AM
Are you using a custom 404 page?

jphilapy
02-28-2006, 09:36 PM
Are you using a custom 404 page?

nope. The only thing I can think of is I am rewriting all html pages to an index.php page. If you try to access a page on the server that does not exist then you are redirected to another page via server rewrites. So in short a page not on the server always appears to be there even when it isn't. Even then I don't see what that has to do with the issue they are pointing out. According to them the page is returning a 200 in the header. So if it is and there is a page showing up on the server, what is the problem?

The fact is when I remove the server rewrites from the htaccess file, they have no problems.

Jeff

jimgem
04-06-2006, 05:23 PM
I'm new at mod rewrite.
I know how to write a line in the htaccess file to make the link "search friendly" as long as the arguments after the "?" has this format: xxx=something&xxx2=something2, but how is it written when the arguments don't have the "=" sign. They are like this: ?xxx&xxx
Thanks for any suggestions.

seomike
04-07-2006, 02:28 PM
Mod rewrite cannot change your urls for you. You'll have to program that.

The rewrite happens on the server when you rewrite

/product-name/01.htm to index.php?product_id=01

blakekr
05-07-2006, 02:18 PM
Hi, Mike, I'm desperate ... sorry for this wordy question. :)

I had to restructure my wordpress site (DOH!) and I'm finding it really hard to enter redirects because of how modrewrite interacts with them.

I think the problem modrewrite statement is:

RewriteRule ^(.+)/([^/]+)(/[0-9]+)?/?$ /index.php?category_name=$1&name=$2&page=$3 [QSA,L]

This is necessary to run my wordpress site with "permalinks", but it makes it (apparently) impossible to redirect like this:

Redirect /2005/12/30/widget-ideas/ http://www.domain.com/themes/themes2/widget-ideas/

... which mod-rewrite turns into this problem:

http://www.domain.com/themes/themes2/widget-ideas/?category_name=2005/12/30&name=widget-ideas&page=

Is there some way I can rewrite this statement to take advantage of the fact that all my old URLs would have numbers and none of my new ones would ... without screwing up how this statement "runs" my current permalinks?

(Thinking hard ... hmm.. .but it IS possible the new ones would occasionally have a number if it showed up on the post title, like "5 ways to wash a car" ... that makes it even harder. Or sometimes posts have titles like "trends for 2006." This is tough)

seomike
05-08-2006, 08:31 PM
Do all your oldd urls follow the same date pattern? and do all your theme folders stay named the same? If so this would work with the redirect from old to new.


# redirect /2005/12/30/widget-ideas/ ---> /themes/themes2/widget-ideas/
RewriteRule ^[0-9]+/[0-9]+/[0-9]+/([^/]+)/?$ http://www.yourdomain.com/themes/themes2/$1/ [R=301,L]

You'll probably want this to be the first rewrite rule in the htaccess.

designasp
05-17-2006, 01:51 AM
Hi SEO Mike,

Thanks for your valuable inputs on the URL rewriting phenomenon.

I haven't had success in my research on my questions and on my other posts to make a final decision. I'm hoping you can help me.

First of all thanks for your time
here's my question...

We know from Google via Matt Cutts that hyphens are the preferred delimiters in URLs

For example
www.widgets.com/bright-new-widgets .....is better than....
www.widgets.com/bright_new_widgets .....or.....
www.widgets.com/bright+new+widgets

For URL rewriting, with .htaccess rewrite rules we can only have a limited number of hyphens. What other character can also be used in this case?

As an example:
www.widgets.com/news-about-bright-new-widgets

In the above example, since the "news-about" is another variable it needs to be delimited with another character to separate it from "bright-new-widgets"

Is using "plus"(+) an option?

www.widgets.com/news-about+bright-new-widgets

Or is there another character which can be used to seperate two words? I have seen many posts on similar questions asked, but no concrete answers. I will be choosing double dashes (--) or (+) as my last resort.

I could make another directory like:
www.widgets.com/news-about/bright-new-widgets
but I prefer not to as I think a flat directory structure is more effective.

Any ideas?

trv
05-17-2006, 07:27 PM
Hello!
I had used to have a site at domain1.com.
There were many files in www.domain1.com/files/ directory.
There are in other sites and other places links to files in that folder.

This site has moved to domain2.com, keeping the old directory structure intact. domain1.com is now used by another site also mine. ( apache2 )

What i want to do is redirect every direct link to

www.domain1.com/files/FILE.ZIP (for example)
to
www.domain2.com/files/FILE.ZIP (this is where the file really is!)

What .htaccess file should i put in /files directory in domain1.com ?

Thank you!

seomike
05-18-2006, 08:01 PM
Hi SEO Mike,

Thanks for your valuable inputs on the URL rewriting phenomenon.

I haven't had success in my research on my questions and on my other posts to make a final decision. I'm hoping you can help me.

First of all thanks for your time
here's my question...

We know from Google via Matt Cutts that hyphens are the preferred delimiters in URLs

For example
www.widgets.com/bright-new-widgets .....is better than....
www.widgets.com/bright_new_widgets .....or.....
www.widgets.com/bright+new+widgets

For URL rewriting, with .htaccess rewrite rules we can only have a limited number of hyphens. What other character can also be used in this case?

As an example:
www.widgets.com/news-about-bright-new-widgets

In the above example, since the "news-about" is another variable it needs to be delimited with another character to separate it from "bright-new-widgets"

Is using "plus"(+) an option?

www.widgets.com/news-about+bright-new-widgets

Or is there another character which can be used to seperate two words? I have seen many posts on similar questions asked, but no concrete answers. I will be choosing double dashes (--) or (+) as my last resort.

I could make another directory like:
www.widgets.com/news-about/bright-new-widgets
but I prefer not to as I think a flat directory structure is more effective.

Any ideas?

Hey thanks for the kind words.

I would suggest this url for the above example /news-about/bright-new-widgets.htm

1 folder deep allows you a lot more control over you rewrite rules.

/news-about/bright-new-widgets = /news-about/bright-new-widgets/index.. and is probably farther than you'd want to be from your root level pages.

The hard thing about doing root level rewrites is making them unique from other files so that normal files don't get pushed through the rewrite rule and mess up your static pages. I hope that makes sense.

You can use + ~ ^ and other meta characters to put into the url to be a unique identifier between variables. but all that can get messy.

Let me know if that answered your questions. And definitely let me know if you need help on the rules

seomike
05-18-2006, 08:09 PM
Hello!
I had used to have a site at domain1.com.
There were many files in www.domain1.com/files/ directory.
There are in other sites and other places links to files in that folder.

This site has moved to domain2.com, keeping the old directory structure intact. domain1.com is now used by another site also mine. ( apache2 )

What i want to do is redirect every direct link to

www.domain1.com/files/FILE.ZIP (for example)
to
www.domain2.com/files/FILE.ZIP (this is where the file really is!)

What .htaccess file should i put in /files directory in domain1.com ?

Thank you!

unfortunately mod rewrite is all domain based in the .htaccess for the domain in which is resides.

You could probably get away with doing a simple frame hack.

on domain1.com/files/FILE.ZIP rewrite it to a static html page on your site that just calls the zip from a frame src. The only thing is, I don't know if that would prompt a download :confused:

I would put the url in a frame src and test it to see if it prompts for a download. If it does I'll help you write the rule that will get it all going :)

ulukach
05-25-2006, 08:56 AM
hi mike,

We have recently followed your advice with apache module rewrite, we could see that when we used [NC,L] (in the .htaccess - RewriteRule) the IE keeps the "abc.html" url address in the address bar & when we use [R=301,L] the IE puts "product.do?p=a&n=b&d=c" in the address bar

so, we obviously wish that the address bar display "abc.html". so when can we return [NC,L]

another thing we're afraid is that we configured it wrongly since when I type
"abc.html" I receive in the reponse header 301 status code with an indication that "product.do?p=a&n=b&d=c" is the permamnent URL.

can you please help - we're afraid to loose SE rank for misconfiguration

Our apache .htaccess is

RewriteEngine on
RewriteBase /

RewriteRule ^products_([^-]+)-([^-]+)-([^-]+)-([^.]+).html$ ProductsAction.do?topicName=$1&categoryName=$2&pageNum=$3&productsOnPage=$4 [R=301,L]

our site is:
babysupply . info



BR
Luk

seomike
05-25-2006, 06:11 PM
The R=301 is telling the server to issue a 301 redirect vs being an alias.

All you need to have is [L] as a flag declaring it's the last rule and you'll be good.

RewriteRule ^products_([^-]+)-([^-]+)-([^-]+)-([^.]+).html$ ProductsAction.do?topicName=$1&categoryName=$2&pageNum=$3&productsOnPage=$4 [L]

ulukach
05-26-2006, 06:58 AM
thanks,

althuogh I will still have to indicate to the SE that
all ".do" requests are permanently rediected to ".html"

I followed your advice from previous post although I must have something wrong (in .htaccess or some httpd.conf flag which I forgot)

RewriteRule ^products_([^-]+)-([^-]+)-([^-]+)-([^.]+).html$ ProductsAction.do?topicName=$1&categoryName=$2&pageNum=$3&productsOnPage=$4

RewriteCond %{REQUEST_URI} ^.*ProductsAction\.do$
RewriteCond %{QUERY_STRING} ^topicName\=([^&]+)\&categoryName\=([^&]+)\&pageNum\=([^&]+)\&productsOnPage\=([^&]+)$
RewriteRule ^.*$ products_%1-%2-%3-%4.html [R=301,L]


My biggest fear is to lose rank if I'll not redirect

BR
Luk

ulukach
05-28-2006, 01:23 PM
hi all,

I think I am misusing module rewrite, I would like to place code which convert static URL to Dynamic (I am placing it in .htaccess)

RewriteEngine on
RewriteBase /
RewriteRule ^foo\.html$ /date.jsp [L]

I would like to place another code snippet which coverts dynamic URL to static (I place it in httpd.conf)

RewriteCond %{REQUEST_URI} ^/date\.jsp$
RewriteRule ^.*$ foo.html [R=301,L]

This causes an infinite loop.
How can I solve this chicken egg dillema?

fakestar
06-04-2006, 10:28 AM
Very helpful this article!

i have some problems with portuguese/latin characters such ç â ã, etc.
I mean, with this rewriterule bellow:

RewriteRule ^article/([0-9]+)/([_A-Za-z0-9-+).html index.php?action=file&fileid=$1 [L]

will change to

http://www.mysite.com/article/47/aço.html

but when i click the link, the browser shows:

http://www.mysite.com/article/47/a%C3%A7o.html

And i get error page, don't load properly

If i replace

RewriteRule ^article/([0-9]+)/([^ç]).html

it will load the page, but i stiil have the url with "a%C3%A7o.html" since browsers does not accept "ç".

Anyways, google will index a page with "ç" or "ã"?

if not, how can i change it to replace "ç" to "c" and show "aco.html"? Is it possible?

i need to change also:

á, é, í ,ó, ú to a, e, i, o, u
â, ê, ô to a, e, o
ã, õ to a, o.

thanks in advance!

Mikkel deMib Svendsen
06-04-2006, 10:35 AM
You should not use special characters - I am from Denmark and we have 3 of them too :) Just drop them and you'll be fine - or convert them "phonetically". In Danish I convert ø to oe æ to ae and å to aa - and even Google recognises this as proper conversions.

JianMing
06-14-2006, 10:44 AM
Hi,

Can anyone help me? I'm new to mod_rewrite. Need to do this:

1) domain redirect - this is working in httpsd.conf for apache server.
2) Need to rewrite any old domain names with "pagename=Singalong" to new server name "pagename=ImadeIt"

example:

http://test.busy.ca/servlet/ContentServer?pagename=Singalong/Home

to

http://test.MadeIT.ca/servlet/ContentServer?pagename=ImadeIt/Home

There are many more URLs that needs to be rewrite base on pagename. I'm at my wits end. How can i do this? :confused:

seomike
06-15-2006, 12:38 AM
RewriteRule ^products_([^-]+)-([^-]+)-([^-]+)-([^.]+).html$ ProductsAction.do?topicName=$1&categoryName=$2&pageNum=$3&productsOnPage=$4

RewriteCond %{REQUEST_URI} ^.*ProductsAction\.do$
RewriteCond %{QUERY_STRING} ^topicName\=([^&]+)\&categoryName\=([^&]+)\&pageNum\=([^&]+)\&productsOnPage\=([^&]+)$
RewriteRule ^.*$ products_%1-%2-%3-%4.html [R=301,L]

Flop your blocks of code. Put the code that catches the old urls first, then the code that does the rewrite second ;)

agateway
06-15-2006, 01:27 AM
Hi,
I hope some one may be able to help. I see in this thread the talk is similar to what I need to do some work on.

Basically I have this code below in my inludes.php what I am trying to do is rewrite the php code for friendly urls.

$Categories .= "<tr>\n\t<td bgcolor=#ECECEC><a class=BlackLinkB href=\"search.php?c=$ai[CategoryID]\">$ai[CategoryName]</a></td>\n</tr>\n";

This php code above bringsup a dynamic nav bar to navigate my site.
I know I need to change the bits in between the "" but I am not sure what I can change it to.

I also need to find out the rewrite code to put in the htaccess file

I am toatlly new to all of this and would love some help here please from some one.

Sincerly, Toni

lumpy
07-23-2006, 02:59 AM
Dear all,

I am trying to create some code that does the following:

somefile.someextension BECOMES somefile_someextension.php5

When I specify the file extension to be converted it works fine:

RewriteRule ^(.*)\.inten $1_inten.php5

But of course I want to set it up so that any file extension is converted.

Can anyone help?

Cau,

lumpy

kenvunz
07-24-2006, 12:32 AM
Hi, I have seen some of the html code where

<img src='/images/abc.gif'> and <img src='../images/abc.gif'>
produce the same result (there is no 'images' folder duplication, only one images folder exist), I believe that this trick can be done by using mod_rewrite (replace / with ../) right ? But how ? Can someone show me how to achieve this? It would save me hours of digging to the html page and changing the source folder.

thanks

bluu
08-03-2006, 03:24 AM
Hi Everybody

My link http://mysite/cafenhe/cafe.php
change to http://mysite/cafenhe/cafe.html

and http://mysite/cafenhe/cafe.php?a=1
change to http://mysite/cafenhe/cafe/1.html

Everybody can help me. I'm newbie to use MOD ReWrite.
Regards.

g1smd
08-09-2006, 07:33 AM
>> somefile.someextension BECOMES somefile_someextension.php5 <<

>> When I specify the file extension to be converted it works fine: <<

>> RewriteRule ^(.*)\.inten $1_inten.php5 <<

I would caution against using underscores in URLs. Use hyphens or dots instead.

The rule you need is:

RewriteRule ^(.*)\.(.*) $1_$2.php5

seomike
08-31-2006, 03:30 PM
Hi,
I hope some one may be able to help. I see in this thread the talk is similar to what I need to do some work on.

Basically I have this code below in my inludes.php what I am trying to do is rewrite the php code for friendly urls.

$Categories .= "<tr>\n\t<td bgcolor=#ECECEC><a class=BlackLinkB href=\"search.php?c=$ai[CategoryID]\">$ai[CategoryName]</a></td>\n</tr>\n";

This php code above bringsup a dynamic nav bar to navigate my site.
I know I need to change the bits in between the "" but I am not sure what I can change it to.

I also need to find out the rewrite code to put in the htaccess file

I am toatlly new to all of this and would love some help here please from some one.

Sincerly, Toni

The quick and easy. to get this url /cat/##.html

PHP code changes

$Categories .= "<tr>\n\t<td bgcolor=#ECECEC><a class=BlackLinkB href=\"cat/$ai[CategoryID].html\">$ai[CategoryName]</a></td>\n</tr>\n";

.htaccess code
RewriteEngine On
RewriteBase /

RewriteRule ^cat/([^/]+)\.html$ search.php?c=$1 [L]

That should do it :)

nikhil
09-07-2006, 11:39 AM
Here is the best way to do it. Using the /?$ will catch the url with or without the trailing slash :)

RewriteEngine on
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQEUST_URI} ^/order.*
RewriteRule ^order/([^/]+)/?$ order/?order=$1 [L]


Hello Mike,

I need a help for .htaccess.

I have a site in that i want to convert the URL name for e.g if the site name is http://www.abc.com/index.php i want the URL to look like this http://www.abc.com/aboutus in the address bar.


Thanks and regards

Nikhil

Mr Flibble
09-08-2006, 11:16 AM
I'm still learning about mod_rewrite rules and having problems trying to understand the basic principles.

Can someone help me rewrite the following:

http://www.url.co.uk/isapi/isapi.dll?page=random_page

to allow this instead as a URL:

http://www.url.co.uk/page.random_page

The trick is the URLs may have Google Adwords parameters so the URL should be able to appear like:

http://www.url.co.uk/page.random_page?gclid=12345678

I guess the rule just needs to capture the portion between "page." and "?" if it exists

Can someone help me with this please? I am going round the bend! :eek:

pobrien
09-20-2006, 05:44 PM
I'm looking for examples to help build the case for doing mod rewrites. This seems to be a greater challenge for ecommerce sites so which have accomplished this? Are there case studies?

Thanks

Paul

grnidone
09-20-2006, 09:26 PM
Check your box, pobrian.

bendede
09-26-2006, 09:23 PM
Will seen : www.abcds.com/yahoo.com
really adres : www.abcds.com/domain=yahoo.com

Code is :
RewriteRule ^(.*)$ /domain.php?domain=$1 [L]

This making to this but now not opening www.abcdsc.com/
Problem is writing "The requested URL /domain.php was not found on this server. "

how can I write code to here.
Thanks

Angela
10-04-2006, 01:09 AM
Hi
Please Could anyone help me sort out my problem, I have read all the thread but still do not find how things work.
I have website with dynamic urls like : http://www.domain.com/books.php?id=about&srch=12
want to redirect them to
http://www.domain.com/novels/

I set a .htacess file with the following :
--------------
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{QUERY_STRING} ^id\=([^&]+)\&srch\=([^&]+)$
RewriteRule ^$ /novels/ [R=301,L]
---------------
But when loading the page it is still the same
Would appreciate your help
Thanks
Angelina

seomike
10-04-2006, 10:46 AM
Will seen : www.abcds.com/yahoo.com
really adres : www.abcds.com/domain=yahoo.com

Code is :
PHP Code:
RewriteRule ^(.*)$ /domain.php?domain=$1 [L]


This making to this but now not opening www.abcdsc.com/
Problem is writing "The requested URL /domain.php was not found on this server. "

how can I write code to here.
Thanks

Try

RewriteRule ^(.*)\.com /?domain.php?domain=$1.com [L]

cbhuvnesh
10-05-2006, 07:19 AM
hello,

please tell me how to rewrite this url

http://www.domain.com/showcatlist.php?browseby=1

to

http://www.domain.com/furniture.html

seomike
10-05-2006, 11:41 AM
cbhuvnesh & Angela

In order to rewrite the above example you would probably have to retain someone's services.

They'd have to look at the database to make sure that "furniture" was a unique identifier that could be used instead of the category or product id.

You'd also have to reprogram the page to output the new urls and then troubleshoot the system.

Simple rewrites like /1.htm rewritten to /index.php?id=1 are easy to post support on.

but rewriting /something.htm to /index.php?id=1 is not possible without seeing the database and code. Even then it may not be possible without database changes and a definite change to your code.

cbhuvnesh
10-05-2006, 11:59 AM
hi,

this is my website http://lefurniture.co.uk/showitemlist.php?id=4&maincatid=1&browseby=1&PHPSESSID=d05b6b4f0aad4d0a09c018009f7ae042



which is a php based dynamic website,

and i want to make all its link in static format and the site should remain dynamic.... its functionally should remain same...

eg:- http://lefurniture.co.uk/bench.html

can u plz suggest me how it is possible.. i tried a lot but cant get succedded..

egain
10-05-2006, 12:09 PM
cbhuvnesh

Seomike hit the nail on the head,
"Simple rewrites like /1.htm rewritten to /index.php?id=1 are easy to post support on.

but rewriting /something.htm to /index.php?id=1 is not possible without seeing the database and code. Even then it may not be possible without database changes and a definite change to your code."

It would be easy for us to suggest answers as to how to fix your problem, however without knowing how your site is setup, doing so could cause more problems than it solves.

I personally have solved problems like what your having with a mix of either URL rewrites or Multiviews and some clever PHP programming. However this was with a thorough understanding of the system and its infrastructure.

nisha1
10-09-2006, 03:11 PM
Hi,

I have a site lets says www.site1.com and I want to have the site1's images and multimedia stuff on the folder called "files" to be retrieved from my other site www.site2.com ( I want to know what Rules and conditions do I have to put under site2.com under .htaccess file.


Currently the site two's contents accessed like this http://www.site2.org/files/filename.fileext I wanted it to be http://www.site1.org/files/filename.fileext

currently site2's htaccess file looks like this.





Options All -Indexes
ErrorDocument 404 /404.php

RewriteEngine On

RewriteRule ^category/([0-9]+).html/?$ index.php?category=$1 [L]
RewriteRule ^news/([0-9]+).html/?$ index.php?news=$1 [L]
RewriteRule ^print/([0-9]+).html/?$ print_version.php?id=$1 [L]
RewriteRule ^pdf/([0-9]+).html/?$ pdf_version.php?id=$1 [L]
RewriteRule ^comments/([0-9]+).html/?$ index.php?comments=$1 [L]
RewriteRule ^author/([^<]+).html/?$ index.php?author=$1 [L]
RewriteRule ^sitemap.xml$ plugins/sitemap/index.php [L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^.]+)/?$ index.php?category_name=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^<]+)/([^<]+).html/?$ index.php?news=$2 [L]

php_flag display_errors On
php_value error_reporting 'E_ALL'



pls let me know what do I have to add to do this.

cbhuvnesh
10-10-2006, 03:05 AM
first of all thanks for responding my post,

plz tell me how to rewrite this url:

http://www.lefurniture.co.uk/showitemlist.php?id=24&maincatid=1&browseby=1

in this way

http://www.lefurniture.co.uk/showitemlist.php/computertable.html

or a similar static link format.....

seomike
10-10-2006, 04:34 PM
Once again...

Those type of url rewrites cannot be done without knowing your backend (database tables/columns) and knowing your programming, otherwise I or someone else would have posted how to do it.


Your code is looking for an ID number (hence: the id=24) in the url. Your code then queries the database using that ID number and tells it to bring back all the information assigned to that ID.

The url you want is this (showitemlist.php/computertable.html) there is no number in it. We cannot tell your php script to query for ID 24 in the database.

These types of rewrites are possible BUT like I said before you're going to have to retain someone to do it for you because you're looking at 4-6 hours of programming and testing to pull it off.

kiko08
10-19-2006, 05:35 PM
hi iam really new to this, i got this tutorial on building a dynamic website using php with no database, its called easy-peasy dynamic php. well from what i understand it just gets the parameters and wich is declared tin the index page.

something like this

// allowed $_GET values
$contentpass = array ('home','tutorial', 'equipment','videos','links','about','contact');
$subconpass = array ('equipment');
$catpass = array ('ball', 'shoes', 'clothes');

// insert the selected content with subcontent
if (in_array($_GET['content'], $subconpass) & in_array ($_GET['cat'], $catpass)) {
include 'equipment/'. $_GET['cat'].'.php';
}

// insert the selected content
elseif (in_array($_GET['content'], $contentpass)) {
include 'contents/'. $_GET['content'].'.php';
}


// insert intro.php as default
elseif (!isset($_GET['content'])) {
include "contents/home.php";
}
// show error page
else {
include "contents/error.php";
}
?>

heres my sample link
dynamic
http://mysite.com/index.php/?content=home
file location
http://mysite.com/contents/home.php

dynamic
http://mysite.com/index.php/?dir=equipment&cat=ball
file location
http://mysite.com/equipment/ball.php

any format would do as long as it will be indexed by search engine easily.
pls help me. thanks!

kiko08
10-20-2006, 07:05 PM
well this is my very firs code with mod rewrite and iam not sure if its correct. can anyone help me?

RewriteEngine On
RewriteBase /

RewriteCond %{QUERY_STRING} ^content\=([^&]+)\&cat\=([^&]+)$
RewriteRule ^$/content/%1-%2.html[R=301, L]

RewriteCond %{QUERY_STRING} ^content\=([^&]+)$
RewriteRule ^$/content/%1-%2.html[R=301, L]

paulgodard
10-24-2006, 08:28 PM
Hi

I am very new to mod_rewrite and I have read a lot about it and I am confused. Moreover simple things do not work.

As many of us, I just need to achieve the following : convert a dynamic url into a static url.

http://domain/Alpha/zPage.php?Content_ID=104&Param_ID=22

http://domain/Page104-22.htm

I have uploaded a .htaccess file into the Alpha directory with the following content :

*Convert static to dynamic url
*All links in site must be in static form
Options +FollowSymLinks
RewriteEngine on
RewriteRule Page(.*)\.htm$ /Alpha/zPage.php?Content_ID=$1&Param_ID=$22

Can someone explain in plain english why this doesn't work and what to do exactly to get it to work?

seomike
10-25-2006, 12:00 PM
I break down a rewrite rule into 4 pieces I'll use your url to show you each part of the 4 pieces.

RewriteRule Page(.*)\.htm$ /Alpha/zPage.php?Content_ID=$1&Param_ID=$22

The action (RewriteRule)
The pattern block (Page(.*)\.htm$)
The rewrite block (/Alpha/zPage.php?Content_ID=$1&Param_ID=$22)
The flag (blank) <-- this is an optional block


There are a few things wrong with the rule.

1. Back referencing - back reference is when you wrap something in parenthesis like (.*). In your pattern block there is only 1. but in your rewrite block there are 2 back references being called. A back reference is called like so $1 for the first () $2 for the second () and so on.

2. regular expression mix up. - regular expressions use metacharacters '.*?+' etc to match patterns of text. the metacharacter '.' means any character or number. The * is an operator that means match the preceding character 0-infinite times. so .* means match any character 0 - infinite times.

Whats happening is instead of getting the 2 numbers 104-22 seperatly your pattern block is snatching them both up including the - inbetween them and passing them to the rewriteblock and putting them in place of the $1. That is why the rule doesn't work at all.

Solution:

All we need to do is tweek 2 things. Correct the pattern block by telling it to reference each number individually. Then just delete the extra 2 off the end of the rewrite block since that would add a 2 to the end of the query and screw things up.

adding a [L] flag block to the end is optional with only one rewrite rule. I'll add it just for fun.

Options +FollowSymLinks
RewriteEngine on
RewriteRule Page(.*)-(.*)\.htm$ /Alpha/zPage.php?Content_ID=$1&Param_ID=$2 [L]

kiko08
10-25-2006, 05:26 PM
hi mike what about mine?

seomike
10-26-2006, 04:27 PM
Whats the name of the file that is receiving the variables? Once I know that I'll be able to help ya :)

kiko08
10-26-2006, 05:17 PM
index.php
more info on my past post.
iam on a subdomain.

heres my sample link
dynamic
http://mysite.com/index.php?content=home
file location
http://mysite.com/contents/home.php

dynamic
http://mysite.com/index.php?dir=equipment&cat=ball
file location
http://mysite.com/equipment/ball.php

any format would do as long as it will be indexed by search engine easily.
pls help me. thanks!

seomike
10-27-2006, 11:11 AM
heres my sample link
dynamic
http://mysite.com/index.php?content=home
file location
http://mysite.com/contents/home.php

dynamic
http://mysite.com/index.php?dir=equipment&cat=ball
file location
http://mysite.com/equipment/ball.php



RewriteEngine On
RewriteBase /

RewriteRule ^contents/([^/]+)\.php index.php?content=$1 [L]


# http://mysite.com/equipment/ball.php
RewriteCond %{REQUEST_URI} !/contents/.*
RewriteRule ^([^/]+)/([^/]+)\.php index.php?dir=$1&cat=$2 [L]



give that a whirl :)

apostolovd
10-27-2006, 12:50 PM
Hi guys,

Very useful tips and tricks!!!

I would like to ask you for your advise:

I have php site with some files like index.php, aboutus.php, contactus.php etc.

and have 4 languages: english, french, spanish and chinese

so I do for example: index.php?lang=fr

For mod_rewrite I use this:

RewriteRule ^en/(.*)$ $1?lang=en
RewriteRule ^cn/(.*)$ $1?lang=cn
RewriteRule ^fr/(.*)$ $1?lang=fr
RewriteRule ^sp/(.*)$ $1?lang=sp

and the link is rewritten to: domain/en/index.php or something like this

but once somebody clicks on the other links the files can not be found there? how can i resolve this?

seomike
10-27-2006, 02:59 PM
try this. you'll only need 1 rule to get the job done.


# rewrites en/index.php -> index.php?lang=en
RewriteEngine On
RewriteBase /

RewriteRule ^([^/]+)/index\.php$ index.php?lang=$1 [L]

apostolovd
10-27-2006, 03:56 PM
try this. you'll only need 1 rule to get the job done.


# rewrites en/index.php -> index.php?lang=en
RewriteEngine On
RewriteBase /

RewriteRule ^([^/]+)/index\.php$ index.php?lang=$1 [L]



Thank you for the fast response!!!

May be I did not explain ti well, what I need is to rewrite:
example:

www.domain.com/aboutus.php?lang=en

to

www.domain.com/en/aboutus

Can you help me please?

kiko08
10-27-2006, 05:00 PM
many thanks mike its working.

manilodisan
11-05-2006, 05:56 AM
I have this rewrite rule:

RewriteRule ^(.*)/(.*)$ $2.php?rewrite_params=$1&amp;page_url=$2

and the following url returns a 404 err. Can anyone figure out why?

http://www.mydomain.com/PHP/category/1/ID/catlistings

flashedcoder
11-10-2006, 10:57 AM
If someone types in the URL http://www.mydomain.com/dogs.en then the server interprets this as http://www.mydomain.com/index.php5?page=dogs&language=en


If someone types in the URL http://www.mydomain.com/cats.fr?z=hello&zip=4 then the server interprets this as http://www.mydomain.com/index.php5?page=cats&language=fr&z=hello&zip=4

how to i write a rewrite rule on those 2 .. i have tried a few tricks mike posted no luck

flashedcoder
11-13-2006, 03:45 AM
still need help thanks

seomike
11-14-2006, 06:38 PM
for your first example this should work

htaccess code

RerwriteEngine On
RewriteBase /

# if .en can be other values then use this rule
# RewriteRule ^(.*)\.(en|fr|es)$ index.php5?page=$1&language=$2 [L]
RewriteRule ^(.*)\.en$ index.php5?page=$1&language=en [L]


for the second example

htaccess code

RewriteEngine On
RewriteBase /

RewriteCond %{QUERY_STRING} ^z=([^&]+)&zip=([0-9]+)
RewriteRule ^(.*)\.(en|fr|es)$ index.php5?page=$1&language=$2&z=%1&zip=%2 [L]

flashedcoder
11-15-2006, 02:28 AM
its giving me 404 error page not found thanks for u help man

flashedcoder
11-15-2006, 09:42 AM
hey it works was a problem at my side mike u are the greatest coder

ignaciogggg
11-16-2006, 02:53 AM
site.com/cv/admin/main.php?m=content

to

site.com/cv/admin/content


note: I put the .htaccess file into "admin" folder?


pls and thanks

i'm from argentina, mike, you're a genius, sorry about my little english

flashedcoder
11-16-2006, 03:51 AM
for your first example this should work

htaccess code

RerwriteEngine On
RewriteBase /

# if .en can be other values then use this rule
# RewriteRule ^(.*)\.(en|fr|es)$ index.php5?page=$1&language=$2 [L]
RewriteRule ^(.*)\.en$ index.php5?page=$1&language=en [L]


for the second example

htaccess code

RewriteEngine On
RewriteBase /

RewriteCond %{QUERY_STRING} ^z=([^&]+)&zip=([0-9]+)
RewriteRule ^(.*)\.(en|fr|es)$ index.php5?page=$1&language=$2&z=%1&zip=%2 [L]

in the above how to make "z=%1" z dynamice meaning it can be any thing other than z which user submits

seomike
11-16-2006, 11:59 AM
RewriteEngine On
RewriteBase /

RewriteCond %{QUERY_STRING} ^([^=]+)=([^&]+)&zip=([0-9]+)
RewriteRule ^(.*)\.(en|fr|es)$ index.php5?page=$1&language=$2&%1=%2&zip=%3 [L]

try this :)

ignaciogggg
11-16-2006, 12:53 PM
pls mike, help me

seomike
11-16-2006, 01:17 PM
site.com/cv/admin/main.php?m=content

to

site.com/cv/admin/content


note: I put the .htaccess file into "admin" folder?


pls and thanks

i'm from argentina, mike, you're a genius, sorry about my little english

RewriteEngine On
RewriteBase /

RewriteRule ^cv/admin/([^/]+)/?$ cv/admin/main.php?m=$1 [L]

put the htaccess file in your root folder.

flashedcoder
11-16-2006, 02:01 PM
read next please this one is solved

flashedcoder
11-16-2006, 02:28 PM
RewriteEngine On
RewriteBase /

RewriteCond %{QUERY_STRING} ^([^=]+)=([^&]+)&(.*)=([^&]+)&(.*)=([^&]+)&(.*)=([^&]+)
RewriteRule ^(.*)\.([^&]+)$ index.php5?page=$1&language=$2&%1=%2&%3=%4&%5=%6&%7=%8 [L]
RewriteRule ^(.*)\.([^&]+)$ index.php5?page=$1&language=$2 [L]

i get values by using this url

http://www.domain.com/cats.ens?x=1&z=2&y=3&s=1

but one problem i am getting is that when i put http://www.domain.com/cats.ens?x=1 it shows no x variable values

ignaciogggg
11-16-2006, 07:50 PM
I have:

RewriteEngine On
RewriteRule ^admin/([^/]+)/?$ admin/main.php?m=$1 [L,NC]

and

<base href="http://www.site.com/">

so... I do echo $_GET['m'] -> "main.php"

why?!

Also, i try with
RewriteEngine On
RewriteRule ^admin/([^/]+)/$ admin/main.php?m=$1 [L,NC]

works only with the last "/"
e.g. http://www.site.com/admin/something/

404 error:
e.g. http://www.site.com/admin/something


--

other error:
http://www.site.com/admin/test.php

if I want to enter to other page, .htaccess thinks that page is also a value from "m"


I hope u understand me, sorry again about my little english.

flashedcoder
11-17-2006, 05:26 AM
i fixed that thing

ignaciogggg
11-17-2006, 11:48 AM
somebody can help me? i'm desperate :(

---

edit:

OK!! I did it!

Options +FollowSymLinks

RewriteEngine On

RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^admin/(.*[^/])/?$ admin/main.php?m=$1 [QSA,L]

That's is the solution.

I'm desperate? not any more, hehehe

gotit
11-17-2006, 04:40 PM
I have been trying to rewrite my url. It is just not working for me.

My URLs read like this:

.com/system/listingView.php?listingCode=1045
.com/system/listingView.php?listingCode=1046
...... and so on

I learned how to make .htaccess file in notepad

I tried some samples and changed the variables around for my own site, didn't work.

appreciate your help

seomike
11-17-2006, 05:31 PM
what do you want the new urls to look like?

gotit
11-17-2006, 06:14 PM
what do you want the new urls to look like?

Any thing that make each page look different, maybe not ending with numbers or end with variables from the page.

I managed to get my page title change according to variables in the page. The title now shows the listing_styel, listing_city, listing_address.
If I can do the same with the url, it will be great.

.com/system/listingview/townhouse(string)/123 main street(string) ------(is a format like this possible?)

cspenn
11-28-2006, 11:40 AM
Somewhat tough one for me.

Starting URL:
domain.com/dir/terms.php?term=$v

Goal:
domain.com/dir/$v/

There is already an index.php file which contains a list of linked terms; the URL for each term is formatted as the goal.

Existing, non-working rule:

RewriteEngine on
RewriteBase /dir/
RewriteRule ^(.*)/$ terms.php?term=$1 [L]

Please help if you can! Thanks!

seomike
11-28-2006, 12:16 PM
RewriteBase defines the root in the rewrite block, bolded below, and not the pattern matching block.

RewriteEngine on
RewriteBase /dir/
RewriteRule ^(.*)/$ terms.php?term=$1 [L]


Try this

RewriteEngine on
RewriteBase /dir/

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^dir/([^/]+)/?$ terms.php?term=$1 [L]

ignaciogggg
12-04-2006, 03:37 PM
How can I do this:

file.php to file.js

file.php:

<?php header("Content-Type: text/javascript"); ?>
//javascript code here!

thinktank
12-06-2006, 10:48 PM
hi everyone,
i'm really not sure if i'm posting in the right thread, but if you could help me with some problems i am encountering since i started studying mod_rewrite (a couple of weeks now), i will be very thankful.

well my problem is that:
before i learned about mod_rewrite, i have a link in one of my page that looks like this:

<a class="thumbnail" href="#thumb"><img src="some image location"></a>

it works! i mean it must not go anywhere if i click on it, i just need the <a> to make some layer hidden and be visible once i hover my mouse on the images (css).

and then came mod_rewrite, while reading some articles on the net i learned about the power of this module, so i started implementing it to my site i am currently developing, so far so good...

then when i click on the images, they brought me back to the main page, they still show the hidden layer once i hover the mouse on them though, but if i click on them they return to the main page which should not be the case. please help me, i'm stuck with this problem for a couple of days now.

btw: i have RewriteBase / in my .htaccess

thank you

sydney30
12-07-2006, 05:20 AM
hi,
i would like to make a 301 to many dynamics urls
the urls i need to redirect look like this :
/news/stats.php?y=06&m=01
/news/login.php?y=06&m=03 and news/index.php?entry=entry051221-114158

i have about hundreds of urls like this from my old blog ..
and since they are dynamic they are not making any 404 .. but duplicate content ..

i really need to buid this redirection ..
is there anyone that can tell me what should i do and what is the code i may use in the .htaccess ?

thanks a lot for your help
syd

Herod
12-19-2006, 03:30 AM
Hi,
I tried everything i found on this forum but still i can't do it..
I want to make this:
http://bangyourbrain.com/source/index2.php?op=show&id=95&mod_rewrite=Noul%20album%20Samael
into this
http://bangyourbrain.com/source/Noul-album-Samael

i have this code:
#start .htaccess code
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^op\=([^&]+)\&id\=([^&]+)\&mod_rewrite\=([^&]+)$
RewriteRule ^$ /%3.html [R=301,L]
ErrorDocument 401 /v-web/errdocs/401.html
please someone help me :(

saud
12-28-2006, 10:56 PM
hiya guys.

what MOD rewrite rule i need to do change my this url :

http://www.pakistani.pk/songs/index.php?dir=English+Songs%2F

to this:

http://www.pakistani.pk/songs/EnglishSongs

and

http://www.pakistani.pk/songs/index.php?dir=English+Songs%2FBaD+BoYs+2%2F

to

http://www.pakistani.pk/songs/EnglishSongs/BaDBoYs.html


ur help will pleasure for me

sharo
01-05-2007, 07:49 AM
Hi all,

I have following url:
http://www.site.com/dir1/dir2/something.jpg my question is, how to change it using mod_rewrite or something else to:
http://www.site.com/view:dir1/dir2/something.jpg or:
http://www.site.com/view:dir1/dir2/something
Please help me

Greetings,
Milen

Ndoki53
01-08-2007, 03:33 PM
I want to change:

http://www.somesite.com/subcat_view.php?cid=2

To this:

http://www.somesite.com/Business_Finance/business.html

I tried the code below and does not work. Please assist me.


RewriteCond %{QUERY_STRING} ^subcat_view\=([^&]+)\&cid=2\=([^&]+)$
RewriteRule ^$ /Business_Finance/business.html [R=301,L]

Grateful

sportbikes
01-13-2007, 03:32 AM
Great info here on the mod rewrite, but I have yet to see how to handle my issue. We are moving servers and the forums will remain on one server, while the balance of the site will be on a seperate server.

An illustration of this would be:

--OLD--
http://www.mydomain.com/forums/newreply.php?do=newreply&noquote=1&p=96251

--NEW--
http://forums.mydomain.com/fourms/newreply.php?do=newreply&noquote=1&p=96251

Note how I need a new "A" record (forums.) on the URL so that it points to a different server (via DNS).

Any input appreciated.

shehabic
02-05-2007, 06:10 PM
Hello This is My 1st Post Here
I Need Help With The Following

sitename.com/something_a-b_c--d-e_f--g-h_i..... (Unlimited Number Of Variables) to

be translated to :

index.php?op=something&a=b&na=c&d=e&nd=f&g=h&ng=i
i used to do it like this :

RewriteRule detailed-listing--(category|type|city|area|purpose|page)-(.*)_(.*)--(category|type|city|area|purpose|page)-(.*)_(.*)--(category|type|city|area|purpose|page)-(.*)_(.*)--(category|type|city|area|purpose|page)-(.*)_(.*)--(category|type|city|area|purpose|page)-(.*)_(.*)--(category|type|city|area|purpose|page)-(.*)_(.*) /index.php? listing=all&$1=$2&n$1=$3&$4=$5&n$4=$6&$7=$8&n$7=$9&$10=$11&n$10=$12&$13=$14&n$13=$15&$16=$17&n$16=$18


BUT The Above works for 6 Arguments And I made Another one for 5 argumnets and a 3rd for 4 arguments ........................................ I Don't Think this is a good way i believe there is much smarter and simpler way . .

seomike
02-05-2007, 06:46 PM
mod_rewrite is not dynamic enough to make rules on the fly like that.

you have to have a rewrite rule per url pattern.

shehabic
02-05-2007, 07:03 PM
So I Must Make One Line For Each Number Of Variables :
like a line if URL is

something--a-b_c
and another for :
something--a-b_c--d-e_f
and a third for :
something--a-b_c--d-e_f--g-h_i

???

Exavior
02-06-2007, 04:59 PM
Hey seomike!

It would seem you are the go-to-guy for info on this subject, and I could really use your help. I tried for a couple hours to get this to work without bothering you, but I can't find and example that matches the same type of sturcture I'm looking for.

www.somesite.com/media/constantly_changeing_folder/news
www.somesite.com/media/constantly_changeing_folder/info
www.somesite.com/media/constantly_changeing_folder/about


to

www.somesite.com/media/constantly_changeing_folder/index.php?page=news
www.somesite.com/media/constantly_changeing_folder/index.php?page=info
www.somesite.com/media/constantly_changeing_folder/index.php?page=about

And I would like the .htaccess file to live here www.somesite.com/media/

The "constantly_changing_folder" is for individual media projects that will be large in number, and I don't want to have to place a .htaccess file in every one of those folders.

Thanks in advance for any help!

seomike
02-06-2007, 06:01 PM
shehabic
You're correct :)


Exavier

I would put the code in the root .htaccess file

You should be able to get by with this rule

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^media/([^/]+)/([^/]+)/?$ media/$1/index.php?page=$2 [L]

Exavior
02-06-2007, 07:00 PM
Using this exact code, in the root of the site: www.somesite.com/.htaccess


RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^media/([^/]+)/([^/]+)/?$ media/$1/index.php?page=$2 [L]


I get a 404 when I go to

http://www.somesite.tv/media/project_name/info/

And the index file that would be processing the data is located at: http://www.somesite.tv/media/project_name/index.php

EDIT:

I removed "RewriteBase /" from the .htaccess file, and it works perfectly now. Could that cause any problems in the future?

Thanks a ton for your help here!

/Exavior

seomike
02-06-2007, 08:51 PM
ops my bad on that.

rewritebase removed won't cause any issues :)

Exavior
02-06-2007, 09:34 PM
Cool deal. And thanks again for the help!

/Exavior

Exavior
02-07-2007, 04:18 PM
Hey mike!

Hate to bug you again, but I ran into an odd bug.

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^media/([^/]+)/?$ media/index.php?project=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^media/([^/]+)/([^/]+)/?$ media/index.php?project=$1&page=$2 [L]


If I go to:
http://www.somesite.com/media/projectname

Sub navigation URLs on that page do not work properly, and target http://www.somesite.com/media/somelink


if I go to:
http://www.somesite.com/media/projectname/

Sub navigation works as desired, targeting
http://www.somesite.com/media/projectname/somelink


The ovarall goal is to be able to go to
http://www.somesite.com/media/ and get a list of all media projects

but if a sub directory is defined, go to that directory as a project, and allow sub directories of that project to be passed for things like news/info/etc

http://www.somesite.com/media/projectname
http://www.somesite.com/media/projectname/news
http://www.somesite.com/media/projectname/info
etc

But on those pages http://www.somesite.com/media/projectname the links to news/info/etc don't work unless you have a "/" and that causes problems if people just type the projectname, since most people don't put the trailing slash.

/Exavior

hedjr01
02-11-2007, 01:33 PM
Hi Mike,

I have tried searching thru the threads for an answer to my situation.

I took one of my HTML pages as a template, inserted a php script, then saved the page as a php file. The script finds articles from a remote site (with their permission of course!) and pastes them into a generated page that matches my theme.

I want to display the following URL example result as a static url:

http://www.thisis-my-siteurl.com/a.php?title=Biology

I tried using a .htaccess file as follows, but it didn't change anthing:

Options +FollowSymLinks

RewriteEngine On
RewriteCond % {SCRIPT_FILENAME} !-f
RewriteRule *(.+)\.html$ /a.php?title=$1 [QSA,L]

Mod_Rewrite does function on my server and I did place the htaccess file in the right place.

Any suggestions?

noeclipse
03-03-2007, 04:13 AM
I still get 404.

my static link: http://www.site.com/phpfilename/nameofperson/idnumber
my dynamic link: http://www.site.com/phpfilename.php?name=nameofperson&id=idnumber

.htaccess location: http://www.site.com/.htaccess
My .htaccess file:
#start .htaccess code
RewriteEngine On

RewriteRule ^phpfilename/([^/]+)/([^/]+)/?$ phpfilename.php?name=$1&id=$2 [L]

----
I do NOT want ".html" at the end
----


This is what I have understood so far about ModRewrite: You enter a static url in addressbar and .htaccess parses the URL and "rewrites" it according to the code given.
Then that goes into the server and the server processes the file with whatever inputs given.

I still get 404 error under IE and "No Input file specified" under FireFox

Then I tried using this:
#start .htaccess code
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^phpfilename/([^/]+)/([^/]+)/?$ phpfilename.php?name=$1&id=$2 [L]

But that did NOT help either.
Any help is appreciated.
Thanks a bunch.

noeclipse
03-05-2007, 03:24 PM
Looks like I have found my answer. I could not delete my last post, so I decided to post my results here.

For anyone who is using GoDaddy as their web host. You will have to wait about 2 - 5 minutes before .htaccess kicks in. :mad: (apparently that seemed to work for me) :cool:

Here is my code

Options FollowSymLinks
RewriteEngine On
RewriteRule ^phpfilename/(.*)/(.*) phpfilename.php?name=$1&id=$2


To make your CSS work with mod_rewrite. Use this:
<style type="text/css">
@import url(http://www.site.com/style.css);
</style>

Cagonni
03-23-2007, 01:27 PM
in godaddy there are some problems with mod_rewrite..

take a look at this..

Options SymlinksIfOwnerMatch
Options +FollowSymLinks -MultiViews
RewriteEngine On
Rewritebase /
RewriteRule ^/?test\.html$ test.php [L]

this htaccess change test.php to test.html it works.But in my seo script zoints seo i should edit this lines like above..

RewriteRule ^([a-z0-9_\-]*-(f|all)[0-9]+(p[0-9]+|/index[0-9]*)?\.html)$ forumdisplay.php/$1 [QSA,L]
RewriteRule ^([a-z0-9_\-]*-(t|p)[0-9]+(p[0-9]+|/index[0-9]*)?\.html)$ showthread.php/$1 [QSA,L]
RewriteCond %{REQUEST_URI} !(index\.php|\.css) [NC]
RewriteRule ^(archive|sitemap)/(.*)$ $1/index.php/$2 [QSA,L]

but how?

AvWeb
03-28-2007, 01:19 PM
Hi Mod Rewrite Gurus...

I have a strange rewrite that I need help with, for an image gallery.

Here's how the system unfolds:

http://www.example.com/art-gallery/index.php?/categories ( main page )

http://www.example.com/art-gallery/index.php?/category/8 ( parent category )

http://www.example.com/art-gallery/index.php?/category/13 ( sub )

...art-gallery/picture.php?/13/category/13( image )

Now, there are quite a few links that have variables like this:

index.php?/blahblah/

I would love to rewrite these as: index-blahblah.html

Any advice?

Thanks!

htaccesselite
03-28-2007, 08:45 PM
Now, there are quite a few links that have variables like this:

index.php?/blahblah/

I would love to rewrite these as: index-blahblah.html


This will rewrite
http://site.com/art-gallery/index-food.html -> /art-gallery/index.php?/food/

RewriteEngine On
RewriteBase /art-gallery/
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^index-([^\.]+)\.html$ index.php?/$1/ [L,NC]

Is that what you mean?

AvWeb
03-28-2007, 09:13 PM
Hi htaccesslite -

Yes, that's exactly what I mean. The rewrite rules you gave me, however, don't QUITE work.

As an example, /index-category-13.html doesn't resolve to the category, but resolves to the index.php main page.

Kind Regards,

Jason

xspeedcruiser
03-29-2007, 09:30 AM
hi mike,
i have been struggling with this mod_rewrite for quite sometime now, someone please help me

i have 3 urls which i want to re-direct
1. http://www.domain.com/mypage should be re-written to http://www.domain.com/index.php?module=mypage

2. http://www.domain.com/mypage/profile should be re-written to http://www.domain.com/index.php?module=mypage&section=profile

3. http://www.domain.com/mypage/profile/edit should be re-written to http://www.domain.com/index.php?module=mypage&section=profile&action=edit

Any help will be greatly appreciated
thanks a lot ppl

StErMi
03-29-2007, 12:23 PM
I'm sorry for my bad english =)

I want to use modrewrite to do this:

http://nickname.domain.com OR http://www.nickname.domain.com

----->

http://www.domain.com/profile.php?nick=nickname

i wish that you understand my question =)

thank you very much

rupakcse
04-06-2007, 05:05 AM
Hello,
My .htaccess code is here.

RewriteEngine On
RewriteRule ^([A-Za-z0-9_/]+)([/]*)$ /test/test.php?title=$1 [PT]

I'm getting "Internal Server Error" with this code.. But it's working in my Localhost [Local System]. Can anyone tell me what may be the problem. And what are the dependencies in server.

Thanks in advance.

helpme101
04-13-2007, 04:11 AM
hi mike,

i've tried many times using different modrewrite in the HTACCESS, but still it not functioning...

i have to change this dynamic page:

http://www.example.com/portfolio/portfolio.php?catID=8


into static, it shows like this:

http://www.example.com/portfolio/portfolio-8.html


please help me how to convert it.. thank you

Eudoxus
04-18-2007, 04:06 PM
hi mike,

i've tried many times using different modrewrite in the HTACCESS, but still it not functioning...

i have to change this dynamic page:

http://www.example.com/portfolio/portfolio.php?catID=8


into static, it shows like this:

http://www.example.com/portfolio/portfolio-8.html


please help me how to convert it.. thank you


Easy enough. I do this on my site. Create a .htaccess file and put in it


Options +FollowSymlinks
RewriteEngine on
RewriteBase /

RewriteRule ^portfolio/portfolio/([^/]+)\.html$ portfolio/portfolio.php?catid=$1

then you should be good to go. ;)

mdg
04-19-2007, 12:34 AM
Hi SeoMike

I have this url:
http://www.example.com/detalle.php?id=902

and i need to get this..

http://www.example.com/pretty-girls.html

I need to rewrite with the name of the url i´ve in that section..

Also i have this url..

http://www.example.com/seccion.php?c=E-mule&s=Pel%EDculas

y need to get this

http://www.example.com/seccion/emule-peliculas

I do this..

Options +FollowSymLinks
RewriteEngine on
RewriteRule detalle-id-(.*)\.htm$ detalle.php?id=$1

but nothing happend

Thanks and i hope u can help me :)

kiko08
05-06-2007, 11:26 PM
Hi SeoMike
Iam really new to modrewite and dont know how do to this.
I have files index.php, news.php and so on...

my urls are like this:
index.php?id=home, index.php?id=about

news.php?id=news1


in index.php i want to include the files in the ./inc/ folder
in news.php i wan to include the files in the ./news/ folder

pls help me.
thanks.

ive tried this code

RewriteEngine On
RewriteBase /

RewriteRule ^inc/([^/]+)\.php index.php?id=$1 [L]

it works on ie and opera but not in firefox. is there any mistake on my code?

purelaser
05-09-2007, 01:49 PM
Hi Guys
Just reading all post, try to rewrite code
from http://example.com/index.php?act=filename
change to http://example.com/filename.html

I try this
#htaccess mod rewrite code
RewriteEngine On
RewriteBase /

RewriteCond %{QUERY_STRING} ^act\=([^&]+)$
RewriteRule ^$ %1.html [R=301,L]

NOT working :(
can any body help me
thanks
ron

v3rtikal
05-13-2007, 04:09 AM
Can someone help me with this issue?

I'm trying to rewrite the following all to one page..

site.com/articles -> articles.php
site.com/articles/category -> articles.php?cat=#
site.com/articles/category/title -> articles.php?cat=#&title=title

I've looked and can't find anything on a setup like this, I've tried this:

RewriteRule ^articles /?$ /articles.php
RewriteRule ^articles/(.*)/?$ /articles.php?cat=$1
RewriteRule ^articles/(.*)/(.*)/?$ /articles.php?cat=$1&title=$2

But sadly it don't work..
Any help greatly appreciated

tutunmayan
05-21-2007, 10:29 AM
Hi I searced the internet, looked many sites but couldnt solve my problem.

here it is:

I want to convert http://john.mysite.com/friends/
to http://mysite.com/index.php?user=john&page=friends
by using mod_rewrite where users like john are dynamic.


Thanks for your helps.

htaccesselite
05-21-2007, 07:25 PM
I'm trying to rewrite the following all to one page..

site.com/articles -> articles.php
site.com/articles/category -> articles.php?cat=#
site.com/articles/category/title -> articles.php?cat=#&title=title


This code uses internal redirects, so when you request http://site.com/finance/homes the address doesn't change in the users browser but /articles.php will see http://site.com/articles.php?cat=finance&title=homes
RewriteEngine On
RewriteBase /
RewriteRule ^articles/?$ /articles.php [L]
RewriteRule ^articles/([^/]+)/?$ /articles.php?cat=$1 [L]
RewriteRule ^articles/([^/]+)/([^/]+)/?$ /articles.php?cat=$1&title=$2 [L]
(removed a keyword stuffed anchor text link)

htaccesselite
05-21-2007, 07:40 PM
I want to convert
john.mysite.com/friends/ -->
mysite.com/index.php?user=john&page=friends
where users like john are dynamic.

RewriteEngine On
RewriteBase /

#loop stopping code
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]

RewriteCond %{HTTP_HOST} !^(www\.)?mysite\.com [NC]
RewriteCond %{HTTP_HOST} ^([a-z]+)?\.mysite\.com$ [NC]
RewriteCond %{REQUEST_URI} ^/([a-z]+)? [NC]
RewriteRule ^([^/]*)?/ /index.php?user=%2&page=%3 [L]

Based on a similar answer found at htaccesselite.com/htaccess/wildcard-dns-and-seo-subdomain-redirecting-internally-vt169.html (removed link)

mailryo
05-21-2007, 08:33 PM
I know someone else asked before but after reading all the replies, didn`t found an answer to that question.so here is it again... thx !

How do I make this redirect ?

http://www.server1.com -> http://12.12.12.12/dir/dir/dir

but keep: http://www.server1.com/dir/dir/dic in the address bar instead of straight http://12.12.12.12/dir/dir/dir

Justlearning
06-01-2007, 02:39 AM
Hello new here, tried to figure it out myself with no luck, so here I am..

I'm trying to do a redirect from the old links in the search engine(s)

So I have say My_Page.php?name=Whatever
Attempting redirect:

RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} ^My_Page.php$
RewriteCond %{QUERY_STRING} ^name=([^&]+)$
RewriteRule ^.*$ %1.html [R]
RewriteRule ([a-z]+)\.html$ My_Page.php?name=$1

So it's something like that, I've tried different variations too, adding flags moving them etc..
I can get the page to redirect but I end up with "Whatever.html?name=Whatever" as the address in the browser.. Any suggestions?.. I didn't know if when doing rewrites if the pages have to be a different name the what they were when the search engines went to them.. Thanks..

htaccesselite
06-01-2007, 02:54 AM
I'm trying to do a redirect from the old links in the search engine(s)

Easy, so what are the old links URI's and where do you want them to be redirected too? In other words, what links do the search engines see that are bad, and where would you like them to be redirected to? Rewriting internally is way different than redirecting.


site.com/this.html --> 301 Redirect to site.com/newthis.php

Justlearning
06-01-2007, 03:59 AM
Not that there bad/ broken or anything.. I just have like category.php?category=media I turned it into "category_list_media.html" then if on page 2 of media it's "category_list_media_2.html" ,etc.. But I want the search engine to get my new links and index those instead of the "category.php?category=media" way.. I didn't change page names or anything as far as the php pages go.. Just the links in the pages..

jeremyleyden
06-01-2007, 10:18 AM
I want this page: http://domain.com/main.php?par2=par3&par4=par5&par6=par7&par8=par9

to look like this: http://domain.com/main/pa2/par3/par4/par5/par6/par7/par8/par9

Here is my code that is NOT working:
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !index/
RewriteRule ^(.*)/(.*)/(.*)/$ /$1.php?$2=$3&$4=$5&$6=$7&$8=$9 [L]

Thanks in advance!

Justlearning
06-01-2007, 04:43 PM
I want this page: http://domain.com/main.php?par2=par3&par4=par5&par6=par7&par8=par9

to look like this: http://domain.com/main/pa2/par3/par4/par5/par6/par7/par8/par9

Here is my code that is NOT working:
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !index/
RewriteRule ^(.*)/(.*)/(.*)/$ /$1.php?$2=$3&$4=$5&$6=$7&$8=$9 [L]

Thanks in advance!
I'll give it a whirl..

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^main/([^/])/([^/])/([^/])/?$ main.php?par1=$1&par2=$2&par3=$3 [L]

AndrewThomas1977
06-02-2007, 01:01 PM
HI there I have tried to follow your very helpful tips here but am failing

This is my page

thegowercoast.com/staying_in_gower/gower_self_catering_cottage.php?id=1

and i am trying to show

thegowercoast.com/staying_in_gower/gower_self_catering_cottage/1.html

any advice would be welcome

Am i correct in saying that it will not change the url output for me but if someone enters the second address it will find the dynamic address and display under the 2nd url i require.
e.g if i have a dynamically generate link
( gower_self_catering_cottage.php?id=1)in the staying_in_gower/index.php page, and select that link it will display the same in the follwing page??

as you may have guessed i need a little clarification on its use..

many thanks

Andrew

Justlearning
06-03-2007, 06:41 AM
Figured it out..

What I was trying to do is get rid of the "?" mark, cause it was leaving the var on the end.. So it would look like "http://somesite.com/example.html?name=example" after it redirected..
So the fix
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} ^.*somepage\.php$
RewriteCond %{QUERY_STRING} ^name\=([^&]+)$
RewriteRule ^.*$ %1.html? [R=301,L]

So adding the "?" mark to the the rule right after the %1.html . Gets rid of it.. ;)

For AndrewThomas1977:
I just started learning this rewrite stuff.. So hope I get it right..lol

Mod rewrite doesn't rewrite the urls in the pages, you have to do that to the page(s) yourself... The mod rewrite matches your link and applies it.. Thats if it matches anything of course..

So: yoursite.com/somepage.php?var1=some&var2=somemore
into: yoursite.com/some/somemore.html
Would be:
RewriteEngine On
RewriteBase /
RewriteRule ^([^/]+)/([^/]+)\.html$ somepage.php?var1=$1&var2=$2 [L]

So when someone clicked "yoursite.com/some/somemore.html", "some" being the first var for somepage.php, and "somemore" being the second var for somepage.php..

Like I said I'm new to this.. But figured I'd try to help, cause I know how it is waiting for an answer..

AndrewThomas1977
06-07-2007, 05:07 PM
thanks for your reply just learning.....i needed that clarification..no to put into place... shall post my solution for others to see as it all helps

paranoid87
06-13-2007, 02:33 PM
thanks..this thread helped me a lot..similar problems..

earnest
06-14-2007, 02:14 PM
PLEASE HELP!!

I would like to permanently move
http://example.com/links/index.php?category=Dentists

to this
http://example.com/dir/dentists.php

and
http://example.com/links/index.php?category=Accounting+%26+Taxes[/url]

to this
http://example.com/dir/accounting.php

What are the exact 301 .htaccess code that i should use?

Thank you

jackson992
06-24-2007, 03:53 AM
Hi all:

I've been trying to get my pages to redirect as all my urls are changing.

I have tried:

Redirect 301 http://jacksretail.com/jcwhitney-links.php?interest=Hitches%2C+Towing+%26+Trailers% 2C+Towed+Vehicle%2C+Tow+Dollies%0D
http://jacksretail.com/jcwhitney-links.php?interest=Hitches%2C+Towing+%26+Trailers% 2C+Towed+Vehicle%2C+Tow+Dollies

For some reason this won't work and I'm removing the extra characters at the end of the first url so I get an empty page at the new url.

Anyone have any ideas?

Marcia
06-24-2007, 03:59 AM
I'm definitely no expert, but I do believe that the first part of that, the URL that's being redirected to the other, isn't supposed to be the full url. The second part is.

Try leaving the second one as is, and changing the first one so it starts with the / (or just the rest of the URL without the / - I'm not certain with that).

I just know that the first part isn't supposed to be the full absolute URL.

simo
07-06-2007, 07:10 AM
how to rewrite this please index.php?option=com_joomlacookbook&Itemid=45&catid=9
many thanks in advance

anzin
07-11-2007, 01:54 PM
i want my site

http://webdesigner.maglente.info/?dir=Application+Interface%2FWiFi+Applications&pic=DunkinDonuts

to look like this

http://webdesigner.maglente.info/Application-Interface/WiFi-Applications/DunkinDonuts.html




Thanks

Sharron
08-09-2007, 12:40 AM
Being a Newbie is stating the obvious! I've a problem with the mod rewrite I'm using in my .htaccess.


RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^/]+/)*([^./]+)/?$ /$2.php [L]

RewriteEngine on[/SIZE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^([^/]+/)*([^./]+)/?$ /$2.html [L]


I THINK, or it does change all my url's to expressionless url's for example

http://www.mysite.com/index.html to http://www.mysite.com/ (http://www.mysite.com/index.html)

or http://www.mysite.com/blahblah.html to http://www.mysite.com/blahblah

However.... I've several links on http://www.mysite.com/ to pages in another folder (sub directory?) on the same server same hosting site. It is all one site, just files in a different folder, which by the way is called interviews.

Such as: http://www.mysite.com/interviews/ now that link works and goes right to the
http://www.mysite.com/interviews/index.html page even without the .html expression.

BUT any other link to pages in that /interviews/ folder do not work, and I get a 404 error page.

Those non working links look like this: http://www.mysite.com/interviews/anypage

I've other questions about then how to get links on the http://www.mysite.com/interviews/(index.html) page to link as well to other pages inside the same /interviews/ folder. But I guess I should tackle one issue at a time.

If anyone has any concrete suggestions or dare I dream have the exact answer to my problem I would appreciate it.

I've asked on another forum on which I've been a long time member, but I just get chastised for even attempting such a thing. lol They say that it will create more problems then benefit of "clean url's". What say the experts?


I'm sure that if this immediate issue is solved I will be indeed in need of a way to rewrite all the existing links to my site pages from elsewhere ie google caches, other sites to reflect or display the new clean url's.

kiko08
08-21-2007, 09:33 AM
can someone pls help me? iam new here.

heres my dynamic url

http://mysite.com/index.php?id=sport&cat=boxing&page=3

i want my url to be like
http://mysite.com/sport/boxing/3.html

heres what will happen....
first it will load the sport.php

./inc/sport.php

then it will load the boxing.php

../allsports/boxing.php

then it will load the page
../allsport/boxing/3.php

hope someone will help me.
tia!

seomike
08-21-2007, 11:34 AM
Just change the php to output desired like (/sport/boxing/3.html)

then just add this to your root .htaccess file


RewriteEngine On

RewriteRule ^([^/]+)/([^/]+)/(.*)\.html$ /index.php?id=$1&cat=$2&page=$3

kiko08
08-21-2007, 04:07 PM
thanks mike youre very helpful to this forum.

i got one problem though with the path.

when i have a url like
http://mysite.com/sport/boxing/3.html

iam using relative path sample is my css file which is located in ../theme/blue/style.css

when iam using modrewrite
it became ../sport/theme/blue/style.css

it will be plenty of work if i put ../../ to all the files and links on my site. is there any easy way to do it? thanks again

seomike
08-21-2007, 04:27 PM
Just make your links to css or js files absolute.
http://www.yourdomain.com/somefile.css for example

kiko08
08-21-2007, 04:34 PM
how about the images and links i have atleast 100 internal links in one page.

seomike
08-21-2007, 05:18 PM
make them all absolute as well. :)

kiko08
08-22-2007, 10:34 PM
yeah thank i guess i have no other choice.
well its done already. ive changed all the path of the links and images.

i have one problem more
i also have a link like this
http://mysite.com/viewimage.php?img=img95.imageshack.us/img95/2627/filenameofimage&cat=hi&f=Eagles

ive tried to use
http://mysite.com/img95.imageshack.us/img95/2627/filenameofimage/hi/Eagles.html

with modrewrite
RewriteRule ^([^/]+)/([^/]+)/([^/]+)\.html$ /viewimage.php?img=$1&t=$2&f=$3

and iam getting 404 error

theskateparkdirectory
10-11-2007, 04:04 PM
I'm trying to rewrite my site to be more search engine friendly but I'm having some troubles.

I have a page, browse.php that serves up locations of different skateparks. Depending on the depth of your browse, I have a different number of variables in the url. Example:

...state=OH

...state=OH&city=columbus

...state=OH&city=columbus&park=TheFlowSkatepark

etc.

I would like to rewrite these so they display as:

.../oh/

.../oh/columbus/

.../oh/columbus/theflowskatepark.html

I need some help. Thanks a lot to anybody that can answer this.

nrgiser
10-22-2007, 04:00 PM
I was trying to add slash (if missed) and after rewrite to /script.php

Basicly it suppose to be like this:
FROM /request => /request+/ => request.php

The source code:

RewriteBase /
RewriteRule ^(.*[^/])$ $1/ [R]
RewriteRule ^([A-Za-z]+)/$ $1\.php [L]


But instead of /script.php it gets me to /script.php/

Could you provide any help how to fix that ? Suggestions ?

aliusman
10-26-2007, 07:11 AM
Hello Mike,

Your article helped me a lot. Thanks for being so helpfull to others. This article is best article I ever see regarding mod rewrite.

You are the BEST teacher.

Thanks again,
Ali

seomike
11-03-2007, 03:29 PM
thx for the kind words :)

Marcia
11-06-2007, 12:31 PM
(BTW, welcome back Mike. And again: thanks for all the great tips on a very tough topic.)

For the folks who have posted in this thread (and read it), can we get your opinion on a question (member poll) about the name, focus and topics of this forum?

Thread is here:

Change Title of Dynamic Web Site Forum? (http://forums.searchenginewatch.com/showthread.php?t=20204)

Thanks to everyone for your input and participation!

ScottJohnson
01-08-2008, 01:25 AM
Thanks Mike for a great and very helpful article!

I'm having a problem that I can't seem to figure out. (Being new to rewrites, that's not surprising.)

I made a rule to rewrite static URL's to dynamic ones so they are more search engine friendly (which are working, thanks to your article's help), but now I need to 301 redirect from the old, dynamic URL's to the new, static URL's so the search engines will not keep crawling the dynamic ones. The problem is that when I do that, the rewrites create an endless cycle: dynamic redirects to static, which rewrites to dynamic, etc... I'm stumped.

Here is the .htaccess file contents with the rules I'm using that are creating the loop:

--- begin .htaccess ---

Options +FollowSymLinks
RewriteEngine On
RewriteBase /

#rewrite static to dynamic
RewriteRule ^(.*)_code_(.*).html$ index.php?code=$2&%{QUERY_STRING} [L] #this by itself works fine

#301 redirect search engines so they index the static URL's
RewriteRule .* http://www.domain.com/productpage_code_39.html? [R=301,L] #adding this creates the endless cycle

--- end .htaccess ---

Is there a way around this or am I just going to have to live with it?

Thanks again,
Scott

seomike
01-08-2008, 02:07 AM
Hey Scott,

RewriteRule .* http://www.domain.com/productpage_code_39.html? [R=301,L] #adding this creates the endless cycle

What this is telling the system to do is rewrite any thing after the .com to that specific page. So it'll loop no matter what file or directory you type.

Can you post the entire old url with the full query string. That will help me in creating the old url to new url redirects.

thx

ScottJohnson
01-08-2008, 03:21 AM
Mike,

Oops, I forgot to include the RewriteCond:

--- begin .htaccess ---

Options +FollowSymLinks
RewriteEngine On
RewriteBase /

#rewrite static to dynamic
RewriteRule ^(.*)_code_(.*).html$ index.php?code=$2&%{QUERY_STRING} [L] #this by itself works fine

#301 redirect search engines so they index the static URL's
RewriteCond %{QUERY_STRING} .*code=39$ [NC]
RewriteRule .* http://www.domain.com/productpage_code_39.html? [R=301,L] #adding this creates the endless cycle

--- end .htaccess ---

That's how it should look.

Here are the full URL's you asked for:

dynamic:
htp://www.domain.com/index.php?code=39&osCsid=1fab54d04c5a8b5a944c94c5388b5e62

static:
htp://www.domain.com/productpage_code_39.html?osCsid=1fab54d04c5a8b5a94 4c94c5388b5e62

(Note: I removed one of the t's from http above so the board wouldn't shorten the URL's with "...")

The osCsid part may or may not be there. I was testing without it though.

Thanks again,
Scott

seomike
01-08-2008, 12:22 PM
Here's a possible solution.

First we have to rename index.php. Change it to index2.php. We don't want index.php to exist on the system otherwise if we rewrite to it it'll cause a loop.

then you can implement these rules to the htaccess


RewriteCond %{QUERY_STRING} ^code=([^&]+)&osCid=([^&]+)
RewriteRule .* /productspage_code_%1.html?osCid=%2 [R=301,L]

add your rewrite to static here. Don't forget to rename index.php to index2.php in your rule :)


Give this a shot :)

dustybrunson
03-16-2008, 12:03 AM
Hey Mike,

Would be so happy i you could help me this , here's what I am trying to do.

I want to be able to have 3 kinds of pretty urls like so

#1 mywebsite.com
#2 mywebsite.com/sectiontitle
#3 mywebsite.com/sectiontitle/pagetitle

what I want is for the url mod_rewrite .htaccess to change these into this

#1 mywebsite.com do nothing
#2 mywebsite.com/index.php?section=sectiontitle but I want the browser window to just show mywebsite.com
#3 mywebsite.com/index.php?section=sectiontitle&page=pagetitle and the browser window shows mywebsite.com

Here's the .htaccess I have been working on, it kindof works

Options +FollowSymLinks
RewriteEngine on
# do not perform rewrite skip next 4 rules
# this doesn't work all files get processed with next 4 rules
# i was trying to determine if there is anything after mywebsite.com
RewriteCond %{REQUEST_URI} !/([^/.]+)$
RewriteRule .? - [S=4]

# stores request in E=reqURI:%1
# this seems to work
RewriteCond %{THE_REQUEST} ^(.*)$
RewriteRule .? - [env=rED:yes,NS]

RewriteCond %{REQUEST_URI} ^(.*)$
RewriteRule ^(.+)$ - [env=varsURL:%1]

# first rewrite rule was match so do redirect
RewriteCond %{ENV:rED} ^yes$

RewriteRule ([^.]+) /~db/jadehill [L]
RewriteRule (.*) index2.php?urlvar=%{ENV:varsURL}

RewriteRule ([^.]+) index.php [L]


Thanks for your help,

Dusty

aliusman
03-27-2008, 08:03 AM
Hi Mike,

I am new to mod rewrite and I need your help in very small issue. I have a working website at "http://www.preciouspetservices.com/" with completed mod rewrite rules and redirections and every thing is working. Now I want to move that site to another site's sub-folder "http://www.preciouspetmoments.com/preciouspetservices/".

Is there any so that I can implement this without having to rewrite all urls and just set the proper path to rewriteBase directive?

Currently I am writing this in my .htaccess file within the "preciouspetservices" folder of doming "http://www.preciouspetmoments.com/".

RewriteEngine on
RewriteBase /preciouspetservices/
AddType application/x-httpd-php .htm .html

RewriteRule ^Pet-Service/([^/]+)\/$ /search_product.html?id=$1 [L]
RewriteRule ^Pet-Service/([^/]+)\/Page\/([^/]+)\/$ /search_product.html?id=$1&page=$2 [L]


ANY HELP WILL BE OBLIGED.

Thanks in advance

DanCristo
05-20-2008, 04:21 PM
Hi Everyone,
I'm trying to understand how I can change a static parameter like _10052_ to another static parameter like _10054_

Example:
www.example.com/folder1/folder2/product3_10052_10002

to

www.example.com/folder1/folder2/product3_10054_10002

Any help is greatly appreciated.

MrJingles
06-24-2008, 11:37 AM
hey. I need url like this and I don't know how to get it. internal server error goes me crazy.
from: http://example.com/file.php?somthing_goes_here
to: http://example.com/file?somthing_goes_here

Thanks for your ideas.

jimbeetle
06-24-2008, 11:53 AM
Dan, sorry I missed this before. I'm not much of a mod rewrite guy. I assume it would be something fairly straight forward with a bit of regex pattern matching.

MrJingles, post what you've tried so far that throws out the error.

Either way, maybe one of the mod rewrite gurus will come along and lend a hand.

seomike
06-24-2008, 12:00 PM
Hi Mike,

I am new to mod rewrite and I need your help in very small issue. I have a working website at "http://www.preciouspetservices.com/" with completed mod rewrite rules and redirections and every thing is working. Now I want to move that site to another site's sub-folder "http://www.preciouspetmoments.com/preciouspetservices/".

Is there any so that I can implement this without having to rewrite all urls and just set the proper path to rewriteBase directive?

Currently I am writing this in my .htaccess file within the "preciouspetservices" folder of doming "http://www.preciouspetmoments.com/".

RewriteEngine on
RewriteBase /preciouspetservices/
AddType application/x-httpd-php .htm .html

RewriteRule ^Pet-Service/([^/]+)\/$ /search_product.html?id=$1 [L]
RewriteRule ^Pet-Service/([^/]+)\/Page\/([^/]+)\/$ /search_product.html?id=$1&page=$2 [L]


ANY HELP WILL BE OBLIGED.

Thanks in advance

just move your .htaccess file to the /preciouspetservices/ folder.

I'll clean up your code here as well.


AddType application/x-httpd-php .htm .html

RewriteEngine on
RewriteBase /preciouspetservices/

RewriteRule ^Pet-Service/([^/]+)/?$ /search_product.html?id=$1 [L]
RewriteRule ^Pet-Service/([^/]+)/Page/([^/]+)/?$ /search_product.html?id=$1&page=$2 [L]


the /?$ will rewrite even if someone doesn't put a trailing slash on the folder name. Yahoo is notorious for sending traffic to a non /.

seomike
06-24-2008, 12:01 PM
Hi Everyone,
I'm trying to understand how I can change a static parameter like _10052_ to another static parameter like _10054_

Example:
www.example.com/folder1/folder2/product3_10052_10002

to

www.example.com/folder1/folder2/product3_10054_10002

Any help is greatly appreciated.

It looks like your urls are already passing through a rewrite. If you want post your .htaccess code or send it to me via private message and I'll take a look.

seomike
06-24-2008, 12:04 PM
hey. I need url like this and I don't know how to get it. internal server error goes me crazy.
from: http://example.com/file.php?somthing_goes_here
to: http://example.com/file?somthing_goes_here

Thanks for your ideas.

What comes after the ? in the current url. That is what we'll have to make into the something_goes_here.

MrJingles
06-24-2008, 12:41 PM
What comes after the ? in the currect url. That is what we'll have to make into the something_goes_here.

It doesn't matter. let's say http://example.com/file.php?id=1, so my dinamic url should be http://example.com/file?id=1

seomike
06-24-2008, 01:07 PM
It doesn't matter. let's say http://example.com/file.php?id=1, so my dinamic url should be http://example.com/file?id=1

you could output example.com/1.html then. the code to rewrite that would be.


RewriteEngine On
RewriteBase /

RewriteRule ^([0-9]+)\.html$ /file.php?id=$1 [L]


That should get you started.

MrJingles
06-24-2008, 01:36 PM
Ok, thanks. Sorry for dum question, but [L], what does it mean?

seomike
06-24-2008, 01:41 PM
[L] just means its the last rule and to stop the modrewrite engine from running anything after it.

MrJingles
06-24-2008, 01:44 PM
without it would be more server load?

seomike
06-24-2008, 04:50 PM
If you have multiple rewrite rules. If it's your only rule you really don't need it.

However mod rewrite is addictive. 1 rule soon turns in to 4-5 :D

MrJingles
06-24-2008, 05:08 PM
Ok, thanks :D

aspardeshi
07-04-2008, 01:19 AM
Hi Mike,

Thats a great post. I have a problem as described below, But I do not know how does it fit into the above please help me know the procedure.

I am in the business of outsourcing. I have around 100 branded products for taking outsourced jobs. I have a list of the branded products keywords.

I have to design a page maybe in html or php whatever you may think fit with contents about outsourcing. let us assume this is the below paragraph:-

"We are doing outsourcing for the product xxxx since last many years. We have all the resources to manage the outsourcing of the product xxxx. So if you have to outsource the requirement for xxxx, kindly contact us with the details"


Now if you take my product list in a text file of 100 product list. I want xxxx in the above paragraph to be replaced by the product keyword typed in search engine.

Now let us take for example I have product "H*P La*se*rjet 10*20" suppose if anybody type in the search engine the above keyword and if that keyword is in my list then the xxxx in the above paragraph in yellow should get replaced with the keyword "H*P La*se*rjet 10*20" and presented to the search engine. The output will look similar to the one given below

"We are doing outsourcing for the product H*P La*se*rjet 10*20 since last many years. We have all the resources to manage the outsourcing of the product H*P La*se*rjet 10*20. So if you have to outsource the requirement for H*P La*se*rjet 10*20, kindly contact us with the details"

Please help me know how to do it ? Any help from SEO Experts is highly appreciated.

Kind Regards

MrJingles
07-11-2008, 08:11 PM
hey Mike. I need some more help.

dinamic url: http://example.com/default.php?a=$cat&amp;b=$sub&amp;sid=$sid

I need: http://example.com/default-$cat-$sub.html?sid=$sid
my code:

[code]
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^a=([a-z0-9_-]+)&b=([a-z0-9_-]+)&sid=([0-9]+)
RewriteRule .* /default-$1-$2-$3.html?sid=$4 [R=301,L]

[code]

Thanks.
Saulius.

kbadeo
07-23-2008, 06:48 PM
Hi! need a bit of help here. basically what I want is to redirect this url http://www.example.com/ to this http://www.example.com/blogs/. I am using a multi blog so I need to do this a;though it seems stupid. I got that to work by using this code:

RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} !^example.com$ [NC]
RewriteRule ^(.*)$ example.com/blogs/$1 [L,R=301]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]


OK so when this thing loads up it gives me the list of blogs and this is the problem. when I click on a link to view a blog it does not go to the link instead ir appends the link to the existing one hence i get: http://www.example.com/blogs/anotherblogihave.com/index.php
any ides on this? thanks in advance!

jimbeetle
07-24-2008, 10:57 AM
Welcome kbadeo.

I looked around and I'm really confused as to how you have things set up. You're using b2evolution and I guess I have to ask, for your purposes, Why? You basically have a series of almost identical sell pages under different domains. Unless you have other plans for the domains, I can't see using b2evolution or any other blog software for this purpose.

I must be missing something, so if you could explain a bit more, maybe I can get passed my block.

(I snipped the links. If you'd like, include the domain in your profile so other folks can take a look at it.)

kbadeo
07-24-2008, 02:47 PM
Welcome kbadeo.

I looked around and I'm really confused as to how you have things set up. You're using b2evolution and I guess I have to ask, for your purposes, Why? You basically have a series of almost identical sell pages under different domains. Unless you have other plans for the domains, I can't see using b2evolution or any other blog software for this purpose.

I must be missing something, so if you could explain a bit more, maybe I can get passed my block.

(I snipped the links. If you'd like, include the domain in your profile so other folks can take a look at it.)

well I'm a doctor and I want my patients or soon to be patients to have a site where they could sign up. I want to create a community of patients.

jimbeetle
07-24-2008, 06:04 PM
Okay, got it. Was just confused by what was there.

I'm not really strong on .htaccess, but can the first part be simplified to this:

RewriteEngine On
RewriteBase /
RewriteRule ^/$ /blogs/ [L,R=301]

Now for the second part, it looks like what you want to do is to check for a bad request and send it back to /index.php if a file or directory doesn't exist. Is that correct?

madensri
08-26-2008, 03:46 AM
Dear Mike,

At first, Thanks for your brilliant post.

i wants to use mod_redirect rule to change :

http://www.example.com/website/?module=portfolio
to
http://www.example.com/website/portfolio/

+++++++++++ and ++++++++++++++++
http://www.example.com/website/?module=portfolio&cate_id=3&page=2
to
http://www.example.com/website/portfolio/3/2/

Thanks,
madensri

Pnemonic
08-26-2008, 07:45 AM
Hi,
I have http://www.host.com/dir
and I need http://www.dir.host.com,

can I do it with mod_rewrite?

mrleeny
09-18-2008, 08:33 PM
Hi Mike (and others)

I have a problem with the view=

I want to rewrite:

Level 1 Category
http://www.trulywoman.nl/archief/index.php?view=category&cid=3
http://www.trulywoman.nl/archief/category/

Level 2 Article
http://www.trulywoman.nl/archief/index.php?view=article&aid=121
http://www.trulywoman.nl/archief/article-title


After some playing, it didn't work out. I think it is the view- (because this variable changes depending on the level i visit.

Any expert who can solve this enormous dillemma?

Jazajay
09-24-2008, 08:11 PM
Hi madensri
My mod_rewrite may be a bit rusty but try -

RewriteEngine On
RewriteRule ^website/portfolio/$ website/?module=portfolio [R=301,L]


Hi Pnemonic
No what you want is a sub domain I am unaware that you can acheive this via mod_rewrite and I'm pretty sure it has to be brought seperatly.

Hi mrleeny
Again it has been a few months since I have done any serious mod_rewriteing but try -

RewriteEngine On
RewriteRule ^archief/(.*)/$ archief/index.php?view=$1&cid=3 [R=301,L]
RewriteRule ^archief/(.*)$ archief/index.php?view=$1&aid=121 [R=301,L]


Any problems let me know.
Jaza

Korak
09-28-2008, 03:51 PM
Hi Everyone... Got one that has me stumped. I'm using Drupal as a CMS on a shared host and want to add a rewrite rule to send users from subdomain.example.com to www.example.com/subdomain while displaying the original address in the address bar. The CMS is already using a mod rule to make clean URL's for search engines and that is part of my confusion. Do I rewrite to a clean url or the original with the question mark? Anyway, here's the relevant bit of the .htaccess file that exists now:

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /

# Rewrite URLs of the form 'x' to the form 'index.php?q=x'.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
</IfModule>

I attempted to include:
# subdomain rule
RewriteCond %{HTTP_HOST} subdomain.domain.xxx$
RewriteCond %{REQUEST_URI} !subdomain/
RewriteRule ^(.*)$ subdomain/$1

When I attempted to include the above, it messed up the ability of the site to work at all. Clearly, I'm a mod rewrite neophyte so any help will be much appreciated!

Korak

mrleeny
09-30-2008, 12:18 PM
_________________________
Hi Jazajay,

Thank you for your reply.

I added the code into my .htaccess and got the following error:

FILE NOT FOUND: home/trulywoman/domains/trulywoman.nl/public_html/archief/index.php
URI:/home/trulywoman/domains/trulywoman.nl/public_html/archief/index.php?view=&aid=121

The script runs on domain.nl/archief and the adres must be:
/home/trulywoman/domains/trulywoman.nl/public_html/archief/index.php

not
home/trulywoman/domains/trulywoman.nl/public_html/archief/index.php

I hope you can help me with this.

Regards,

Tom

Jazajay
10-06-2008, 03:14 AM
Hi Tom.
Sorry for the late response I've been on a mini break and then I had an issue logging in.

Umm....
Ok which directory is your .htaccess file in?
home?
public_html?
Another one?

Jaza

alexandruc
10-31-2008, 02:54 AM
Hello,

I am new with this stuff so please be gentle with me :)

I have this structure on my WAMP server with mod_rewrite enabled:
http://localhost/bar/prod-bar.php
http://localhost/bar/prod-bar.php?categorie=some-thing-here <- can be 1 word or several words separated by "-"

in the .htaccess (on root - localhost) i have the following code:
Options +FollowSymLinks
RewriteEngine on

RewriteBase /

Rewriterule ^bar/prod-bar/([^/])/?$ /bar/prod-bar.php?categorie=$1 [L]

shouldn't it work?

accordingly with the information I found on the web it should make the links like this:
http://localhost/bar/prod-bar
http://localhost/bar/prod-bar/some-thing-here

the behavior:
1. enter site:
http://localhost/bar/ <- loads index.php page

2. navigate to: prod-bar.php
http://localhost/bar/prod-bar <- displays prod-bar.php

3. navitate to: prod-bar.php?categorie=some-thing-else
http://localhost/bar/prod-bar/some-thing-else <- STILL displays prod-bar.php as if no $_GET is passed.

What am I doing wrong? is there something missing from the .htacces ? is the regex wrong? Can anyone help me with the code please?


thanks,
Alex

p.s: another question is: after i get this to work, should I change the links in the php page code to this format? also do I have to modifiy the $_GET? (i told you i am new :p so dont laugh)

Jazajay
10-31-2008, 03:02 AM
Try changing this line -

Rewriterule ^bar/prod-bar/([^/])/?$ /bar/prod-bar.php?categorie=$1 [L]

to

Rewriterule ^bar/prod-bar/([.*])/?$ /bar/prod-bar.php?categorie=$1 [L]

alexandruc
10-31-2008, 04:23 AM
I replaced the code, but it still acts as if no $_GET is passed.

do i have to change something in the php code for it to work?

currently i have on file prod-bar.php something like:

...
$query = "SELECT cat_name FROM categorii";
$result = mysql_query($query);
while(list($cat_name)= mysql_fetch_row($result)){
echo "<a href=\"/bar/prod-bar/$cat_name\" >".str_replace("-", " ", $cat_name)."</a><br>";
}
...

- this generates the links to the category

so when the link: http://localhost/bar/prod-bar/someting is clicked, something should be passed with $_GET to $categorie.
$categorie = $_GET['categorie'];
if ($categorie !== NULL) {
$query = ...
}

Jazajay
10-31-2008, 09:24 AM
I hope that that is not all your code and there is some cleaning involved?

Sorry I edited your example -
Rewriterule ^bar/prod-bar/([.*])/?$ /bar/prod-bar.php?categorie=$1 [L]

Needs to be
RewriteRule ^bar/prod-bar/(.*)/?$ bar/prod-bar.php?categorie=$1 [L]

No square brackets.
The problem was because the match any character (.) 0 or more times (*) was in the square bracket it was being treated literally, as that is all you want to match be it a full stop character and an asterix character.

Now it is out of the square brackets it has it's specail meaning back.

Edit:
Secondly you had a blackslash at the beginning of the proper url. That had to be removed.

Now that should put everything into the number 1 variable ($1)and give it to the category get variable on the prod-bar.php file in the folder called bar.

Let me know if you are still having issues.

Jaz

alexandruc
10-31-2008, 12:21 PM
first I want to thank you for helping :)

second... it still does not work

I hope that that is not all your code and there is some cleaning involved?

well.. it's not all the code of course.. but i dont really know what you mean by cleaning.

if you are talking about the variable passed in the db, i use mysql_real_escape_string($categorie):

$query = "SELECT something FROM tabela WHERE categorie='".mysql_real_escape_string($categorie)."'";
$result = mysql_query($query);
if (mysql_num_rows($result)==0) { echo "error";
}

otherwise i dont know what you mean by cleaning :)

Regarding the rewriterule.. well it still acts as if no $_GET is passed in the link.

Do I have to edit the php with something for the GET to recognise the value in the link?

Jazajay
10-31-2008, 12:48 PM
Ok alexandruc
No when you referance a url such as your-site/bar/prod-bar/hello

And you echo out $_GET['categorie'] hello would be printed to the page does that make sense?

Is the .htaccess file in the same directory as the bar folder?

Because that code should work the only thing I can think of is the file is not referanced properly.

On the php page just write at the beginning of the body tag -
<?php echo"get=".$_GET['categorie'];?>

If it is not ouputing I would say it is a referance problem, that being that the file it is pointing at can not not be found.

alexandruc
10-31-2008, 12:55 PM
as i said in my first post:
in the .htaccess (on root - localhost) i have the following code:

i moved it now to into the bar folder.

i used the echo thing, but get= (NULL). so indeed the variable is not passed

how can i fix it?

EDIT:

sorry, i miss read your instruction. i moved back the .htaccess file to the root (localhost on WAMP), next to the bar folder(basicaly the site folder)

useing the classic link: http://localhost/bar/prod-bar.php?categorie="something" successfully passed "something"

EDIT 2:

just found something by accident, and i think it's relevant:

i was playing with different configurations of .htaccess and I changed the default index (DirectoryIndex prod-bar.php).

i entered the site.. and as expected the page http://localhost/bar/ displayed the page "prod-bar.php"

when I click the category link.. SURPRISE! the variable was passed and the page http://localhost/bar/prod-bar/something displayed correctly :)

why? and what can I do so that it will work without the index change in .htaccess (i dont want that page to be loaded first as you enter the site)

EDIT 3:
I honestly DON'T know what i did else then deleting the index change (so restoring the .htaccess to the previous state when it did not work), but now it works.

now the var_dump show the variable passed.. and everything works fine.. and i have done nothing but changeing the index then restoring it the way it was!

I hope it's not just something temporary.


EDIT 4 (and last):

I figured it out. it seems for the rewriterule to work (at least on WAMP) you MUST allow folder browsing in the .htaccess

It wasn't the changed index, but the following code in. htaccess that made the rewrite work:
Options All +Indexes

Thanks a LOT Jazajay for your help!!!

Jazajay
11-05-2008, 09:04 PM
Hi Alexandruc
Please do not take my absense to your problem as a negative reason on my behalf.

I honestly did not get an email confirming that you had made a post, I'll empty my bin folder more often I think. :)

I'm glad to here that you have sorted it out and I'm glad I was some help to your problem.

I'll hopefully talk to you soon.

Jaza

georgescifo
03-15-2010, 06:33 AM
Thanks for sharing it..really informative..