views:

56

answers:

1

Is it possible to make a short URL rule so that every word (except the root folders) after http://mydomain.com/ will get redirected to /index.php?permalink=$1 ?

examples:

http://mydomain.com/ - goto index.php (standard). http://mydomain.com/word - goto index.php?permalink=word

My .htaccess looks like this right now:

Options +FollowSymLinks

RewriteEngine On
RewriteBase /

RewriteRule /page/([0-9]+) /?page=$1
+1  A: 

Try this:

  RewriteRule ^.*/(\w+)\b$ /index.php?permalink=$1

^ Assert position at the beginning of a line.
.*/ Match any single character or nothing, until the last /.
(\w+) Match letters, digits, underscores, at least one char, and capture it as a group number 1.
\b Assert position at a word boundary.
$ Assert position at the end of a line.

Thus:

http://mydomain.com/word1 becomes /index.php?permalink=word1
http://mydomain.com/publish_files/word2 becomes /index.php?permalink=word2

For your request on comments, try this, but I haven't tested it yet:

  RewriteCond %{HTTP_REFERER} !^(http://)?{HTTP_HOST}/[^/]+/.*$ [NC]
  RewriteRule ^.*$ /index.php
  RewriteCond %{HTTP_REFERER} !^(http://)?{HTTP_HOST}/\w+$ [NC]
  RewriteRule ^.*/(\w+)$ /index.php?permalink=$1
Vantomex
Thank you for replying. Could you please explain your regular expression?It works, but not quite as I imagined. When I use a word that is not a folder in the root it works. But when I type a word that is a folder I get redircted to http://mydomain.com/publish_files/?permalink=publish_files. I do not understand why it decides to add "?permalink=publish_files" after the URL.The optimal would be if I could decide in the regex which words should be ignored. Is this possible?
cvack
In your recent case (when you use a word that is a folder), what is your expectation to be? Would you edit your question to include your recent case? I will explain my regex after your problem solved entirely.
Vantomex
I'm just curious to why it adds "?permalink=word" after the URL if the word is a folder. If its possible to remove this. Otherwise it looks like it works fine. :)
cvack
I have given an explanation of the regex.
Vantomex
thanks! :) But how do I change it so that http://mydomain.com/word become /index.php?permalink=word and http://mydomain.com/publish_files become /index.php ? :S
cvack
I have edited my answer again. Please try my experimental `RewriteRule`, but I haven't tested it yet. Therefore, please give a feedback whether it works or not.
Vantomex