Am I misunderstanding what RewriteCond does?

I don’t understand why my RewriteRule is not working. I’ve read a lot of pages about htaccess and RewriteCond and settled on this pattern. What I want to do is change the URL appearance from test.html to better-SEO.html. I put the .htaccess file in the same directory as test.html.

This is what I came up with:

Options +FollowSymLinks 
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %(REQUEST_FILENAME} !-d

RewriteRule ^test\.html$ better-SEO.html [L]

This results in a file not found error and test.html is still seen in the URL field.

Am I misunderstanding what RewriteCond does? I thought it renames the URL without redirecting you to a real URL.

(I’m also very confused as to what I’m supposed to put in the field on this site above + Create Topic. Pretty much nothing I enter is matched. Not intuitive.)

Do you have a “better-SEO.html” file in the root folder?

And I think you may be thinking backwards. What happens is more like

test.html/big/red/widget/ is sent to test.html?size=big&color=red&item=widget

It is not meant to “change the URL seen in the browser address bar”
It is meant to allow “pretty URLs” to be used to go to “messy looking URLs” (or alternate files)

Hi SH,

There isn’t anything wrong wtih your code which would stop it from working. HOWEVER, I do not understand why you’d use % (PHP character code - which Apache does NOT understand).

I believe that your question’s title shows a basic lack of understanding of Apache’s mod_rewrite. Rather than repeat a lengthy tutorial here, I’ll merely refer you here. It has plenty of real world examples from SitePoint members’ questions…

Specifically, a RewriteCond is a condition which is ANDed with the regex of the following RewriteCond and subsequent RewriteRule for the rule to be executed.

Finally, more information (resulting redirection? You can force that to be displayed with the R=301 flag. I would think that, if the URL does NOT change, Apache is throwing the error because of the % in the first RewriteCond’s regex.

Mitt was probably overthinking your question and getting deeper into mod_rewrite (see the tutorial) but he is quite correct in the purpose of mod_rewrite: Redirect upon certain conditions. Also on just one of MANY typical uses.

Regards,

DK

1 Like

That answers my question quite succinctly. I misunderstood what it does.

So what’s happening then, is that internally the link is test.html/big/red/widget/ (this is what they see when they rest their cursor over the link) and through htaccess, the server is requested to pull test.html?size=big&color=red&item=widget It doesn’t prettify the resulting link but the internal link. You need the latter URL, not the former URL.

That was the information I didn’t quite get in the materials I had read; only the how-to parts.

OK, now I got it. Thanks!

Thanks for responding, dklynn. I’ve bookmarked the page and will study it further.

To explain to those who come upon this page:

We can use an .htaccess file to show SEO-friendly names in internal links to open links in your directory that are SEO non-friendly links.

Example:
If I have this SEO-friendly internal link on my page:
< p>< a href="**testing.html**">test< /a>< /p>

… and this rule in .htaccess in the same directory as index.html (testing.html does not actually exist as a file):
_ RewriteRule ^ testing.html$ index.html [L]

… then the server will respond to clicking by pulling index.html, but will show testing.html in the browser URL box.

I thought there was a way to show an SEO-friendly URL in the browser URL window in place of the non-friendly URL. This is the way it is done!

NOTE: make sure that scripts do not depend on the name of the file, or they won’t run. (One of my page’s slideshow JS detection scripts plays depending on the name of the file. The RewriteRule changes the file to testing.html and that name and the old name needs to be in the script to keep it working.)

Following example for .htaccess file – internal link “Home-Page-Announcements.html” will open the file named “index.html” that’s listed in your directory:

Options +FollowSymLinks 
RewriteEngine On
RewriteRule ^Home-Page-Announcements.html$ index.html [L]

^ = start matching URL pattern here
$ = stop matching URL pattern here
[L] = if the rule matches, stop looking

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.