Forum Moderators: phranque
The last thing I would like to do is have 'example.com/page?search=answer&submit=submit' to appear as 'example.com/page/answer'. I tried:
RewriteRule ^page/(.+)?$ page?search=$1&submit=submit [L]
I also tried a few other things which didn't work.
I had my own configuration which wasn't working properly. I ended up finding an example that was posted from jdMorgan, that fixed the problems I was having.
So here is what's in my .htaccess:
RewriteEngine on
#
# externally redirect client /index page requests to "/"
RewriteCond %{the_REQUEST} ^[A-Z]+\ /([^/]+/)*index?
RewriteRule ^(([^/]+/)*)index\.php?$ http://example.com/$1 [R=301,L]
#
# externally redirect client requests contains php extension to extensionless URL
RewriteCond %{the_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*[^.]+\.php?
RewriteRule ^(([^/]+/)*[^.]+)\.php?$ http://example.com/$1 [R=301,L]
#
# externally redirect non-blank canonical hostname requests to non-canonical hostname
RewriteCond %{HTTP_HOST} !^(example\.com)?$
RewriteRule (.*) http://example.com/$1 [R=301,L]
#
# if requested extensionless URL-path does not resolve to an existing directory
RewriteCond %{REQUEST_fileNAME} !-d
# and if requested extensionless URL-path plus ".php" does resolve to an existing file
RewriteCond %{REQUEST_fileNAME}.php -f
# then append ".php" to resolve the actual filename
RewriteRule ^(([^/]+/)*[^./]+)$ /$1.php [L]
Thank you to anyone that can lend me a hand. :)
[edited by: jdMorgan at 12:38 pm (utc) on May 20, 2009]
[edit reason] example.com, formatting [/edit]
Options -MultiViews mod_speling or AcceptPathInfo (on Apache 2.x) could also cause the same kind of problem.
Jim
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ http://example.com/$1 [R=301,L]
The problem is that testing for 'directory exists' is inefficient, because a call must be made to the OS file manager --possibly invoking a read of the physical disk-- for each and every HTTP request, unless steps are taken to prevent that.
So, I'd suggest 'listing' any directories that you know exist and using a 'skip rule' construct, so as to avoid doing that filesystem check whenever possible:
# Skip next rule if known or existing subdirectory, or if no trailing slash
RewriteCond %{REQUEST_URI} ^/(w3c/¦forum/¦stats/¦cgi-bin/¦.*[^/])$ [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [S=1]
# Externally redirect to remove trailing slash if not an existing subdirectory
RewriteRule ^(.+)/$ http://example.com/$1 [R=301,L]
Jim
RewriteRule ^page/(.+)$ page?search=$1&submit=submit [L]
As I said in my original post, I want 'example.com/page?search=answer&submit=submit' to appear as 'example.com/page/answer'.
As it is, it doesn't seem to have any affect.
Also what exactly does 'RewriteRule ^ - [S=1]' do? Could you direct me to a proper guide explaining the parameters for RewriteCond and RewriteRule?
Thank you jdMorgan.
There's nothing apparently wrong with your code. So...
How did you test it? (What did you type in?)
What results did you expect?
What results did you get?
How did the actual results differ from those that you expected?
Jim
I had a typo in my test, it does work, I can now access the page from both URLs. So the problem left is that when the user submits the form on example.com/page, it goes to example.com/page?search=answer&submit=submit and not example.com/page/answer.
So what I would like is to have example.com/page?search=answer&submit=submit redirect to example.com/page/answer. But if I do that then the user is going to see example.com/page?search=answer&submit=submit in the address bar first, then it'll redirect? I'd like example.com/page?search=answer&submit=submit never to be shown to the user at all.
preg_match("/page\?answer=(.+)&submit=submit/", $_SERVER['REQUEST_URI'], $match);
if($match[1])
{
header('Location: http://example.com/page/'.$match[1]);
}
Thanks again for all your help jdMorgan. :)
To be clear on "rewrites" - a rewrite does not 'make' a URL. URLs are defined by the links on webpages. After a link is clicked, the browser sends the URL information to the server, and the server uses it to fetch the file. A rewrite changes the server path to get the data from a different place.
Use Live HTTP Headers for Firefox to see "how" it "goes" there.
It is likely a redirect, probably of the 302 type.
Note that "goes to" is not at all a clear description of what happens, when we have explicit words like "redirect" and "rewrite" available to concisely describe the situation.
But then again, if this can be done properly through mod_rewrite, I'd much rather it that way. Unfortunately I still don't know how.
In general there will be several redirects to force canonicalisation, and to redirect direct client requests for the dynamic URL, and there will be a rewrite to accept a URL request in the "static" format and rewrite it to fetch the content from an internal dynamic filepath.
SEO URL's is something that is an old concept. Most of the time people are hurting themselves more than helping. If your making a new website and it is not much work to make "SEO URL's" then do it. Do not spend lots of time changing URL's.
If you ever talk to an SEO company and they want to change your URL's run away.
So instead of modifying your script to force a redirect, modify it to output the "pretty" URLs by re-formatting the path information taken from your database into a pretty form when it is 'building' a URL to put into a link on the page. You can use preg_replace if needed to do this.
If the URLs for links are taken direct from your database, that makes it even easier: Just change your URLs in your database.
Jim