views:

78

answers:

3

The web server is Apache. I want to rewrite URL so a user won't know the actual directory. For example: The original URL:

www.mydomainname.com/en/piecework/piecework.php?piecework_id=11

Expected URL:

piecework.mydomainname.com/en/11

I added the following statements in .htaccess:

RewriteCond %{HTTP_HOST} ^(?!www)([^.]+)\.mydomainname\.com$ [NC]
RewriteRule ^(w+)/(\d+)$ /$1/%1/%1.php?%1_id=$2 [L]

Of course I replaced mydomainname with my domain name. .htaccess is placed in the site root, but when I access piecework.mydomainname.com/en/11, I got "Object not found".(Of course I replaced mydomainname with my domain name.)

I added the following statements in .htaccess:

RewriteRule ^/(.*)/en/piecework/(.*)piecework_id=([0-9]+)(.*) piecework.mydomainname.com/en/$3

Of course I replaced mydomainname with my domain name. .htaccess is placed in the site root, but when I access piecework.mydomainname.com/en/11, I got "Object not found".(Of course I replaced mydomainname with my domain name.)

What's wrong?

A: 

You are doing it backwards. The idea is that you will give people the friendly address, and the re-write rule will point requests to this friendly, non-existent page to the real page without them seeing it. So right now you have it only handling what to do when they go to the ugly URL, but you are putting in the friendly URL. since no rule exists for when people put the friendly URL directly, apache looks for it and says "Object not Found"

So add a line:

RewriteRule piecework.mydomainname.com/en/(*.) ^/$3/en/piecework/$3?piecework_id=([0-9]+)(.*)

Sorry, that's quite right, but the basic idea is, if they put in the URL you like, Apache is ready to redirect to the real page without the browser seeing it.

Update

I'm way to sleepy to do regex correctly, so I had just tried my best to move your example around, sorry. I would try something more simple first just to get the basic concept down first. Try this:

RewriteRule www.mydomainname.com/en/piecework/piecework\.php\?piecework_id\=11 piecework.mydomainname.com/en/11

At the very least, it will be easier to see what isn't working if you get errors.

Anthony
After I added your line, I get "server error".
Steven
Did you remove your other line first? Because if you left that one, it would keep redirecting the user back to this rule, which is redirecting it back to that rule, causing a 500 Internal Server Error for internal rewrite looping.
animuson
I added solely your line, and I get "server error".
Steven
As I said right after, I'm pretty sure I did a terrible job there, I just copied your code and moved things to the opposite sides. Try a more basic experiment first, have a flat, not regex ugly URL rewritten from a friendly URL. I'll post what I mean to the end of my answer.
Anthony
You can see it by yourself:http://piecework.tenxian.com/en/11
Steven
A: 

Here's a quick overview of what's happening:

RewriteCond %{HTTP_HOST} ^(?!www)([^.]+)\.mydomainname\.com$ [NC]

First, the question mark is supposed to be at the end.

$1 would (should) match anything that is not 'www' 0 or 1 times.

$2 matches anything that is not a character 1 or more times, which theoretically would match a blank space there but likely would never match anything.

Then it requires '.mydomainname.com' after those two groupings.

Your first two conditions are looking for two separate groupings.

I'm not sure exactly how you're trying to set up your structure, but here is how I would write it based on your original and expected URL's:

RewriteCond %{HTTP_HOST} !^www\.mydomainname\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(\w+)\.mydomainname\.com$ [NC]
RewriteRule ^(\w+)/(\d+)$ /$1/%1/%1.php?%1_id=$2 [L]

Basically, your first condition is to make sure it's not the URL beginning with 'www' (it's easier to just make it a separate rule). The second condition makes it check any word that could possibly be in front of your domain name. Then your rewrite rule will redirect it appropriately.

Get rid of the last .htaccess line there in your question...

Someone please correct me if I typed something wrong. I don't remember if you have to have the '\' in front of '\w' and '\d' but I included them anyways.

animuson
When I access piecework.mydomainname.com/en/11(Of course I replaced mydomainname with my domain name.), I still get "Object not found".
Steven
Do you have your server set up properly to redirect all your domains back to the main domain? If you just simply created a subdomain named 'piecework.mydomainname.com' then it's running on a separate 'vrtual' server in its own directory and when it's redirecting to the file it's returning a 404 File Not Found because that file doesn't exist in that account.
animuson
When I simply access piecework.mydomainname.com, I get "Due to a configuration error the site you are requesting is not available. "
Steven
Sounds like you have an Apache server problem somewhere. Did you write out all your VirtualHosts by hand or use a program like cPanel or DirectAdmin, etc?
animuson
I only upload .htaccess to the root directory of my website.
Steven
A: 

Try using RewriteLog in your vhost or server-conf in order to check the rewriting process. Right now you just seem to guess what mod_rewrite does.

By using RewriteLogLevel you can modify the extend of the logging. For starters I'd recommend level 5.

PS: Don't forget to reload/restart the server after modifying the config.

stefan