SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Wizard dragonfly_7456's Avatar
    Join Date
    Dec 2002
    Location
    Moscow, USSR
    Posts
    1,092
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Mod rewrite - keyword injection

    Hello fellow sitepointers.

    I am hoping to run a small seo experiment on my website, and was hoping mod_rewrite could help me out.

    I wanted to quickly inject all my urls with a specific keyword, and give the search engines a 301 redirect heads up.

    Tell me please if this is possible.

    I gave it a try, but I'm awful at regex, and I'm not exactly sure how to rewrite on-page links either.

    I tried something like this:

    RewriteRule ^(?!keyword_){0}&&(?!index){0}(.*)\.html$ http://www.mysite.com/$1$2 [R=301,L]

    The idea was to simply add my "keyword" to all pages (except index.html), so that page1.html turned into keyword_page1.html

    But this didn't work at all for me, because I don't think I even got it write in the regex ((
    Content Writing Service - Get custom SEO articles for 10$

  2. #2
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,650
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    Preveet!

    Add a keyword to ALL URIs is something that YOU have to do with your links! mod_rewrite's job is to UNDO the keyword so the original (i.e., existing) file can be served. Otherwise, you've got to rename all your files and that would be painful!

    Okay, you've got a good specification but a weird way to (attempt to) write it with regex. I know that Apache mod_rewrite's regex engine should be able to do all the fancy non-greedy and look-aheads but I prefer to think of it as a "dumbed down" version of regex (because of the simplicity of the URI string - and other Apache variables).
    Quote Originally Posted by dragonfly_7456 View Post
    The idea was to simply add my "keyword" to all pages (except index.html), so that page1.html turned into keyword_page1.html
    Okay, as explained at the top, mod_rewrite's job is to redirect the keyword_pageX.html to pageX.html (because keyword_pageX.html would result in a 404). Therefore:
    Code:
    RewriteEngine on
    
    # keyword is a fixed, Latin word, i.e., not Cyrillic, not accented, just ASCII
    RewriteRule ^keyword_([a-zA-z0-9]+)\.html$ $1.html [R=301,L]
    # IMHO, SE's will NOT like this as they're being deceived
    # with the link requested NOT being the link served
    # - and the R=301 tells them that!
    
    # if keyword is variable, then the _ must be used as a marker
    # (to ignore everything before it)
    RewriteRule ^([a-zA-z0-9]+)_([a-zA-z0-9]+)\.html$ $2.html [R=301,L]
    
    # if my basic assumption is wrong, then we have another problem, i.e.,
    # redirecting everything (except index.html) to keyword_pageX.html
    RewriteCond %{REQUEST_URI} !^index\.html$
    RewriteCond %{REQUEST_URI} !^keyword_
    RewriteRule ([a-zA-z0-9]+)\.html$ keyword_$1.html [R=301,L]
    WARNING! Pick ONLY one of the mod_rewrite blocks above - do NOT blindly insert all three because it'll loop (v1/v2 with v3)!

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  3. #3
    SitePoint Wizard dragonfly_7456's Avatar
    Join Date
    Dec 2002
    Location
    Moscow, USSR
    Posts
    1,092
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Tried the last block and I'm trying to figure out why the redirect didn't work as expected...

    http://www.mydomain.com/
    redirected to
    Code:
    http://www.mydomain.com/home.php/root/www/mydomain.com/keyword_
    This is the block I used:
    Code:
    # if my basic assumption is wrong, then we have another problem, i.e.,
    # redirecting everything (except index.html) to keyword_pageX.html
    RewriteCond %{REQUEST_URI} !^index\.html$
    RewriteCond %{REQUEST_URI} !^keyword_
    RewriteRule ([a-zA-z0-9]+)\.html$ keyword_$1.html [R=301,L]
    Content Writing Service - Get custom SEO articles for 10$

  4. #4
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,650
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    df,

    The empty URI does not match the RewriteRule (mod_rewrite block) statement so it's NOT that statement which is causing the bad redirection.

    That said, the redirection of a RewriteRule will first attempt to match the physical path before it will try to match the the DocumentRoot-related path. Use %{DOCUMENT_ROOT}/{redirection} to get rid of that www/mydomain.com/keyword nonsense - when you find the offending mod_rewrite block.

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •