1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

FAQ: mod_rewrite, 301 redirects, and optimizing Apache.

Discussion in 'Apache' started by Nintendo, Jul 30, 2005.

  1. leeds1

    leeds1 Peon

    Messages:
    585
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #201
    great thread - I have I think a simple mod rewrite

    from: /directory/file1.html
    to: /directory/file1/

    there are about 30 as 301 redirects

    any ideas?
    thanks
     
    leeds1, Feb 6, 2006 IP
  2. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #202
    Options +Indexes
    Options +FollowSymlinks
    RewriteEngine on
    RewriteBase /
    RewriteRule ^directory/([^.]+)/$ directory/$1.html [L]
     
    Nintendo, Feb 6, 2006 IP
  3. leeds1

    leeds1 Peon

    Messages:
    585
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #203
    thanks, I have another one to do with SSL

    Ok, so I have the standard 301 redirect in my .htaccess to redirect non www to www and that's works AOK


    now I have introduced a SSL cert for my order pages but the cert is for the domain ie without the www (eg: domain.com is the cert)

    so when you do https:// www. domain.com it says cert doesnt match because the original 301 redirect kicks in.

    I only want the SSL in one directory, say domain.com/dir1/

    how do I do?

    at the moment I have done this:

    RewriteCond %{SERVER_PORT} ^80
    RewriteCond %{HTTP_HOST} ^domain\.co.uk [NC]
    RewriteRule ^(.*)$ http://www.domain.co.uk/$1 [L,R=301]
    
    Code (markup):
    to ensure the 301 only works with non SSL stuff

    then I have just done the link to the SSL page as domain.co.uk/dir1/

    and of course it works

    but I want the URL to show with the www as well (and if folks come in from whatever place, to turn (worst case scenario) http://domain.co.uk/dir1/ into https://www.domain.co.uk/dir1/

    any ideas

    many thanks
     
    leeds1, Feb 8, 2006 IP
  4. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #204
    I'm not sure how to change the https part, except redirecting http:// to https:// using 301 redirect.
     
    Nintendo, Feb 8, 2006 IP
  5. theblade24

    theblade24 Active Member

    Messages:
    84
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    73
    #205
    I believe a suimple call to your SSL cert issuer to have them reissue for www domain is your easiest and best bet. Comodo reissued mine with no hassels.

     
    theblade24, Feb 8, 2006 IP
  6. leeds1

    leeds1 Peon

    Messages:
    585
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #206
    OK, will do
    thanks
     
    leeds1, Feb 8, 2006 IP
  7. mariush

    mariush Peon

    Messages:
    562
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #207
    
    function url_prepare($s)
    {
    	//return str_replace(array(" ","/","'","\""),".",$s);
    	$s2 = "";
    	$stringlen = strlen($s);
    	
    	if ($stringlen <1 ) 
    	  return "";
    	else
    	{
    	 $i =0;
    	 while ($i <$stringlen)
    	 {
    	 	$i=$i+1;
    	 	$c = $s{$i-1};
    	 	if (($c>="a") and ($c<="z"))
    	 	  $s2 .=$c;
    	 	else
    	 	 if (($c >="A") and ($c <="Z"))
    	 	  $s2 .=$c;
    	 	 else
    	 	  $s2 .="."; //change "." to whatever you want
    	 }
      }
      // if ".." apears somewhere, make only 1 "." 
      $s2 = str_replace("...",".",$s2);  
      $s2 = str_replace("..",".",$s2);
      // if the text was only numbers and signs and all were removed add at least one dot
      if ($s2=="") $s2 = ".";
      return $s2;  
    }
    
    PHP:
    I'm using the above function to convert an article title to words delimited by a dot.

    Ex. Introduction to 5 operating systems -> Introduction.to.operating.systems

    for those of you perfectionists: I know it's not the best programming, could have used regexp, cases and so on and so forth but it does the job.
     
    mariush, Feb 10, 2006 IP
  8. ian_ok

    ian_ok Peon

    Messages:
    551
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #208
    I can't get this redirect to work...

    Options +Indexes
    Options +FollowSymlinks
    RewriteEngine on
    RewriteBase /

    RewriteRule ^folder/page.php?imageID=([^.]+)\&type=listing h ttp://w ww.site.com/folder/page.php [R=301,L]

    imageId=upto 3 digit number I don't need the number for the imageID as this page will be deleted once I get the redirect to work, it's just for good SEO to not report 404's if anyone visits and old link from a SE

    While these work fine:
    RewriteRule ^folder/ref([^.]+)\.html$ h ttp://ww w.site.com/folder/file.php [R=301,L]


    Ian
     
    ian_ok, Feb 12, 2006 IP
  9. DSR

    DSR Member

    Messages:
    77
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #209
    Question: I have a few variables that use a + symbol in them. For example: whatever.com/directory/category.php?cat=something+somethingelse . Is it OK to leave the + if i change these pages to a static looking page? Example, changing the above url to: whatever.com/directory/category/something+somethingelse.html or shoudl i remove the + ? Do search engines dislike + symbols in urls? Thanks! :)

    EDIT: It seems the mod rewrite works for variables without + symbols. Any variables that have the + on my site won't rewrite to a static address :( Anyway around this?
     
    DSR, Feb 12, 2006 IP
  10. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #210
    I don't know if it'll work, but try adding a \ before the +.

    Or post the .htaccess code, if the + isn't in .htaccess.


    RewriteRule ^folder/page.php?imageID=([^.]+)\&type=listing$ http://www.site.com/folder/page.php [R=301,L]

    it might be just missing the $

    And every page would go to the same page, folder/page.php
     
    Nintendo, Feb 13, 2006 IP
  11. ian_ok

    ian_ok Peon

    Messages:
    551
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #211
    Still doesn't work, even when I change the file to an exact number and then visit that page id it still doesn't redirect: The section in red is the one I have problems with.

    [EDIT]Just tried this:
    RedirectMatch 301 /folder/viewimage.php.* http://www.site.com

    And it works but also takes the value after the '?' how can I NOT take the value?
    [/EDIT]


    AddType application/x-httpd-php .php .htm .html
    
    php_value session.use_trans_sid 0
    
    Options +Indexes
    Options +FollowSymlinks
    RewriteEngine on
    RewriteBase /
    RewriteRule ^folder/pic_ref_z/([^.]+)\.php$ http://ww w.site.com/folder/sanlucar_de_barrameda_rental_ref_z.php [R=301,L]
    RewriteRule ^folder/pic_ref_y/([^.]+)\.php$ http://ww w.site.com/folder/sanlucar_de_barrameda_rental_ref_y.php [R=301,L]
    RewriteRule ^folder/pic_ref_x/([^.]+)\.php$ http://ww w.site.com/folder/sanlucar_de_barrameda_rental_ref_x.php [R=301,L]
    RewriteRule ^folder/pic_ref_w/([^.]+)\.php$ http://ww w.site.com/folder/el_puerto_de_santa_maria_rental_ref_w.php [R=301,L]
    RewriteRule ^folder/pic_ref_v/([^.]+)\.php$ http://ww w.site.com/folder/ref_v.php [R=301,L]
    RewriteRule ^folder/pic_ref_u/([^.]+)\.php$ http://ww w.site.com/folder/ref_u.php [R=301,L]
    RewriteRule ^folder/pic_ref_n/([^.]+)\.htm$ http://ww w.site.com/folder/ref_n.html [R=301,L]
    RewriteRule ^folder/pic_ref_m/([^.]+)\.htm$ http://ww w.site.com/folder/ref_m.html [R=301,L]
    RewriteRule ^folder/pic_ref_L/([^.]+)\.php$ http://ww w.site.com/folder/sanlucar_rental_ref_L.php [R=301,L]
    RewriteRule ^folder/pic_ref_j1/([^.]+)\.html$ http://ww w.site.com/folder/rota_rental_apartment_ref_j1.html [R=301,L]
    RewriteRule ^folder/pic_ref_j/([^.]+)\refj.htm$ http://ww w.site.com/folder/ref_j.html [R=301,L]
    RewriteRule ^folder/pic_ref_i/([^.]+)\refi.htm$ http://ww w.site.com/folder/ref_i.html [R=301,L]
    RewriteRule ^folder/pic_ref_h/([^.]+)\refh.htm$ http://ww w.site.com/folder/ref_h.html [R=301,L]
    RewriteRule ^folder/pic_ref_g1/([^.]+)\.php$ http://ww w.site.com/folder/sanlucar_de_barrameda_villa_rental_ref_g1.php [R=301,L]
    RewriteRule ^folder/pic_ref_g/([^.]+)\refg.htm$ http://ww w.site.com/folder/ref_g.html [R=301,L]
    RewriteRule ^folder/pic_ref_f/([^.]+)\reff.htm$ http://ww w.site.com/folder/ref_f.html [R=301,L]
    RewriteRule ^folder/pic_ref_e1/([^.]+)\.php$ http://ww w.site.com/folder/conil_rental_ref_e1.php [R=301,L]
    RewriteRule ^folder/pic_ref_e/([^.]+)\refe.htm$ http://ww w.site.com/folder/ref_e.html [R=301,L]
    RewriteRule ^folder/pic_ref_d1/([^.]+)\.php$ http://ww w.site.com/folder/sanlucar_de_barrameda_rental_ref_d1.php [R=301,L]
    RewriteRule ^folder/pic_ref_d/([^.]+)\refd.htm$ http://ww w.site.com/folder/ref_d.html [R=301,L]
    RewriteRule ^folder/pic_ref_b1/([^.]+)\.php$ http://ww w.site.com/folder/sanlucar_de_barrameda_rental_ref_b1.php [R=301,L]
    RewriteRule ^folder/pic_ref_a1/([^.]+)\.php$ http://ww w.site.com/folder/sanlucar_de_barrameda_rental_ref_a1.php [R=301,L]
    RewriteRule ^folder/pic_ref_o/([^.]+)\.htm$ http://ww w.site.com/folder/grazalema_apartment_ref_o.html [R=301,L]
    RewriteRule ^folder/pic_ref_a/([^.]+)\.php$ http://ww w.site.com/folder/ref_a.html [R=301,L]
    RewriteRule ^folder/pic_ref_b/([^.]+)\.php$ http://ww w.site.com/folder/ref_b.html [R=301,L]
    RewriteRule ^folder/pic_ref_c/([^.]+)\.php$ http://ww w.site.com/folder/ref_c.php [R=301,L]
    RewriteRule ^folder/pic_ref_c1/([^.]+)\.php$ http://ww w.site.com/folder/jerez_de_la_frontera_rental_ref_c1.php [R=301,L]
    RewriteRule ^folder/pic_ref_f1/([^.]+)\.php$ http://ww w.site.com/folder/grazalema_rental_accommodation_ref_f1.php [R=301,L]
    RewriteRule ^folder/pic_ref_h1/([^.]+)\.html$ http://ww w.site.com/folder/bornos_rental_ref_h1.html [R=301,L]
    RewriteRule ^folder/pic_ref_k/([^.]+)\.php$ http://ww w.site.com/folder/ref_k.html [R=301,L]
    RewriteRule ^folder/pic_ref_p/([^.]+)\.php$ http://ww w.site.com/folder/ref_p.php [R=301,L]
    RewriteRule ^folder/pic_ref_q/([^.]+)\.php$ http://ww w.site.com/folder/ref_q.php [R=301,L]
    RewriteRule ^folder/pic_ref_r/([^.]+)\.php$ http://ww w.site.com/folder/ref_r.php [R=301,L]
    RewriteRule ^folder/pic_ref_s/([^.]+)\.php$ http://ww w.site.com/folder/ref_s.php [R=301,L]
    RewriteRule ^sierra_properties_for_sale/ref([^.]+)\.html$ http://ww w.site.com/folder/listing_browse.php [R=301,L]
    RewriteRule ^costa_properties_for_sale/ref([^.]+)\.html$ http://ww w.site.com/folder/listing_browse.php [R=301,L]
    
    RewriteRule ^folder/pic_ref_([^.]+)\/([^.]+)\_ref([^.]+)\.html$ http://ww w.site.com/folder/listing_browse.php?location=Inland [R=301,L]
    RewriteRule ^folder/pic_ref_([^.]+)\/([^.]+)\_ref([^.]+)\.html$ http://ww w.site.com/folder/listing_browse.php?location=Coastal [R=301,L]
    RewriteRule ^folder/pic_ref_([^.]+)\/([^.]+)\_ref([^.]+)\.htm$ http://ww w.site.com/folder/listing_browse.php?location=Inland [R=301,L]
    RewriteRule ^folder/pic_ref_([^.]+)\/([^.]+)\_ref([^.]+)\.htm$ http://ww w.site.com/folder/listing_browse.php?location=Coastal [R=301,L]
    
    [color=red]RewriteRule ^folder/viewimage.php?imageID=377&type=listing$ http://ww w.site.com/folder/listing_browse.php [R=301,L][/color]
    
    redirect 301 /file.html http://ww w.site.com/costa_rentals.php
    redirect 301 /folder/sanlucar.html http://ww w.site.com/costa/sanlucar_de_barrameda.php
    redirect 301 /folder/costa_ballena.html http://ww w.site.com/costa/costa_ballena.php
    redirect 301 /folder/grazalema.html http://ww w.site.com/sierra/grazalema.php
    redirect 301 /sales.html http://ww w.site.com/folder/listing_browse.php
    redirect 301 /folder/ref_o.php http://ww w.site.com/folder/grazalema_apartment_ref_o.html
    redirect 301 /folder/pic_ref_i1/1.html http://ww w.site.com/folder/sevilleruralaccommodation_ref_i1.html
    redirect 301 /sotogrande_golf_villa_for_sale.php http://ww w.site.com/sotogrande
    
    Code (markup):
     
    ian_ok, Feb 13, 2006 IP
  12. DSR

    DSR Member

    Messages:
    77
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #212
    Good advice, but i don't think that is what i'm lookign for. Here is my code:

    Options +Indexes
    Options +FollowSymlinks
    RewriteEngine on
    RewriteBase /
    RewriteRule ^cars/(.*)\.html$ cars/cat.php?category=$1 [L]
    Code (markup):
    The $1 has variables with + in them. example: man+woman . For whatever reason, it won't rewrite the link. I can only figure it's the + symbol because anything without it works fine with the code above. Any suggestions? :confused: :)
     
    DSR, Feb 13, 2006 IP
  13. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #213
    (.*) is supposed to cover everything!!! Try
    RewriteRule ^cars/(.*)+(.*)\.html$ cars/cat.php?category=$1+$2 [L]
    RewriteRule ^cars/(.*)\.html$ cars/cat.php?category=$1 [L]

    If that works, try ([^.]+) instead. That's better than that code.

    Post a few current URLs and where you want them to go.
     
    Nintendo, Feb 13, 2006 IP
  14. Solicitors Mortgages

    Solicitors Mortgages Well-Known Member

    Messages:
    2,217
    Likes Received:
    139
    Best Answers:
    0
    Trophy Points:
    103
    #214
    hey Leeds, this may do it.....although i am not 100% these are the ports you use.

    RewriteEngine On
    RewriteBase /
    
    #For non ssl ports
    RewriteCond %{HTTP_HOST} !^www\.yourdomain\.com
    RewriteCond %{SERVER_PORT} ^80
    RewriteRule (.*) http://www.yourdomain.com/$1 [R=301,L]
    
    #For ssl ports
    RewriteCond %{HTTP_HOST} !^www\.yourdomain\.com
    RewriteCond %{SERVER_PORT} ^443
    RewriteRule (.*) https://www.yourdomain.com/$1 [R=301,L]
    Code (markup):
    :)
     
    Solicitors Mortgages, Feb 13, 2006 IP
  15. DSR

    DSR Member

    Messages:
    77
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #215
    Man, excellent thinking on yoru part but no go :(

    Is it possible for me to set .htaccess to strip out the + symbol in the variable and replace it with somethign else that works?
     
    DSR, Feb 13, 2006 IP
  16. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #216
    You would have to edit the php script to search and replace + with for example _ or nothing. Something like

    $whatever = preg_replace('/+/mi', '_', $whatever);

    (Post in the php board.)
     
    Nintendo, Feb 13, 2006 IP
  17. DSR

    DSR Member

    Messages:
    77
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #217
    Thanks dude...

    Ok, so i went back an dlooked at my coding and my database. It's not an actual + on the databse, it's a space. So for example:

    whatever/cat.php?cat=variable1+variable2 can work the same as whatever/cat.php?cat=variable1%20variable2

    Now, does this make it easier to get this mod_rewrite to work with the %20? Is it easy to remove the %20 and turn the link into a search engine friendly link? :)
     
    DSR, Feb 13, 2006 IP
  18. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #218
    Search and replace can fix it. Space to -.
     
    Nintendo, Feb 14, 2006 IP
  19. ian_ok

    ian_ok Peon

    Messages:
    551
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #219
    Something like from:
    h t tp://www.costaandsierra.com/property_details/viewimage.php?imageID=376&type=listing

    h t tp://www.costaandsierra.com/property_details/viewimage.php?imageID=139&type=listing

    h t tp://www.costaandsierra.com/property_details/viewimage.php?imageID=609&type=listing

    GOING TO:
    h t tp://www.costaandsierra.com/
    OR
    h t tp://www.costaandsierra.com/property_details/listing_browse.php
     
    ian_ok, Feb 14, 2006 IP
  20. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #220
    
    Options +Indexes
    Options +FollowSymlinks
    RewriteEngine on
    RewriteBase /
    RewriteRule ^property_details/viewimage.php?imageID=([^.]+)&type=listing$ http://www.costaandsierra.com/property_details/listing_browse.php [R=301,L]
    RewriteRule ^property_details/viewimage.php?imageID=([^.]+)&type=listing$ http://www.costaandsierra.com/ [R=301,L]
    
    Code (markup):
     
    Nintendo, Feb 14, 2006 IP