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 :(((

Tried the last block and I’m trying to figure out why the redirect didn’t work as expected…

redirected to

http://www.mydomain.com/home.php/root/www/mydomain.com/keyword_

This is the block I used:


# 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]

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

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).

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:

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