Re: accidental SE friendly urls

Discuss practical ways rearrange URLs using mod_rewrite.

Re: accidental SE friendly urls

Postby msinternet » Mon Jun 23, 2008 7:16 am

Hi,

I am hoping someone can point me in the right direction. I am making a CMS and I want it to have URL rewrite SE friendly urls. The native urls are in the form:
Code: Select all
http://www.mysite.com/index.php?language=english&page=index.html


in this case I want the url rewritten to:

Code: Select all
http://www.mysite.com/english/index.html


But:

I only want it to do it if there is no real file and I want it to rewrite to

Code: Select all
http://www.mysite.comlish/index.html
if the language get variable is not used.

I also want it to redirect from some old urls.

I have so far done this:

Code: Select all
Redirect 301 /printabout.htm http://www.mysite.com/
Redirect 301 /printassembly.htm http://www.mysite.com/assembly.htm
Redirect 301 /printcontact.htm http://www.mysite.com/contact.htm

Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*[^/]+/(.*)\.htm\ HTTP/
RewriteRule (.*)/(.*) index.php?language=$1&page=$2  [L]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.*)\.htm\ HTTP/
RewriteRule (.*\.htm*) index.php?page=$1 [L]


It works but I am not sure if it should as I tried it on another server and it did not work. The [L] flag does not seem to work properly as what happens after has an effect where it shouldn't.

I want it to go something like:

If pattern is http://www.mysite.com/???/???.html translate to
http://www.mysite.com/index.php?languag ... e=???.html and STOP.

Elseif pattern is http://www.mysite.com/???.html translate to http://www.mysite.com/index.php?page=???.html

Also the redirects do wierd things. I tried adding a [L] to the redirects and it broke. I just want them to redirect and do nothing else.

If anyone can shed some light on this that would be great as I am a bit stumped by this.

Thanks,

Martin
Website design and Develoment
msinternet
 
Posts: 3
Joined: Mon Jun 23, 2008 7:02 am

Postby richardk » Tue Jun 24, 2008 2:16 pm

Do the redirects with mod_rewrite, as well
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteRule ^printabout\.htm$    http://www.mysite.com/             [R=301,L]
RewriteRule ^printassembly\.htm$ http://www.mysite.com/assembly.htm [R=301,L]
RewriteRule ^printcontact\.htm$  http://www.mysite.com/contact.htm  [R=301,L]

RewriteRule ^([^/]+)/([^/]+\.html)$ /index.php?language=$1&page=$2 [L]
RewriteRule ^([^/]+\.html)$         /index.php?page=$1             [L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby msinternet » Wed Jun 25, 2008 1:21 am

Thanks. I see what you mean, that's much better.

Will that do the conditional stuff where one thing will happen if there is a subdirectory and another if not.

Also, will the rules be skipped if the file actually exists?

Martin
msinternet
 
Posts: 3
Joined: Mon Jun 23, 2008 7:02 am

Postby richardk » Thu Jun 26, 2008 1:02 pm

Will that do the conditional stuff where one thing will happen if there is a subdirectory and another if not.

The optional language parameter? It should do.

Also, will the rules be skipped if the file actually exists?

No, but neither would your original.

Add
Code: Select all
# Stop if it's a request to an existing file.
RewriteCond %{SCRIPT_FILENAME} -f
RewriteRule .* - [L]

after
Code: Select all
RewriteRule ^printcontact\.htm$  http://www.mysite.com/contact.htm  [R=301,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby msinternet » Fri Jun 27, 2008 12:54 am

Thanks,

that's great. One little hitch is that it gives me a forbidden 403 error if I go to /Spanish/ and do not enter a page such as index.html. That's no bother though as I will just use the full url's.

Martin
msinternet
 
Posts: 3
Joined: Mon Jun 23, 2008 7:02 am

Postby richardk » Sat Jun 28, 2008 4:29 pm

One little hitch is that it gives me a forbidden 403 error if I go to /Spanish/ and do not enter a page such as index.html.

Adding
Code: Select all
RewriteRule ^([^/]+)/?$             /index.php?language=$1         [L]

to the end might fix it.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am


Return to Friendly URLs with Mod_Rewrite

Who is online

Users browsing this forum: No registered users and 102 guests

cron