Forum Moderators: phranque

Message Too Old, No Replies

I need help with htaccess URL Rerwrite

htaccess URL Rewrite

         

joshuaceo

6:13 pm on Apr 8, 2009 (gmt 0)

10+ Year Member



How can I make these type of urls:

www.website.com/about-us.html
www.website.com/services.html
www.website.com/another-service-option.html
www.website.com/services/service2.html

Rewrite to these type with htaccess:

www.website.com/about-us/
www.website.com/services/
www.website.com/another-service-option/
www.website.com/services/service2/

I do not want the folders to exist. I just want it to look like they really do exist with url rewriting. can anyone help me with this?

jdMorgan

6:27 pm on Apr 8, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



See this recent thread [webmasterworld.com] to get started. Then post your best-effort code and test results as a basis for discussion.

Please see our Apache Forum Charter [webmasterworld.com] for more information.

Thanks,
Jim

joshuaceo

6:32 pm on Apr 8, 2009 (gmt 0)

10+ Year Member



I came up with this code but it did not work:

rewriteEngine on
rewriteRule ^about-us\.html /about-us/

Anyone know what I am doing wrong?

jdMorgan

6:48 pm on Apr 8, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, you're going backwards.

mod_rewrite cannot "change" a URL; Only the links on your pages define the URL.

What mod_rewrite *can* do is to connect your new "about-us/" URL to the existing "about-us.html" file on your server.

It's a two-step process: You must change the links on your pages to the extensionless format, and then add code to .htaccess to connect those new URLs to your server files. After that is done, there's an optional third step, best not addressed until the first two are working.

The cited thread goes into some detail about this.

Jim

joshuaceo

6:58 pm on Apr 8, 2009 (gmt 0)

10+ Year Member



Ok I have turned my urls to extensionless format. example:

about-us
services

what is the htaccess code to connect the extensionless files to the new urls

about-us/
services/

joshuaceo

7:09 pm on Apr 8, 2009 (gmt 0)

10+ Year Member



RewriteCond %{REQUEST_URI} !(\.[^./]+)$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) /$1./ [L]

this is hard =( jdMorjan how much do you charge for making a .htaccess to do what I am looking for?

g1smd

9:10 pm on Apr 8, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



If this is backwards:

RewriteRule ^about-us\.html /about-us/

then reverse it.

jdMorgan

9:55 pm on Apr 8, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Not too hard, and the overhead charges on something simple like this would kill ya! ;)

# Rewrite extensionless non-blank URL requests to add ".html" if corresponding ".html" file exists:
# If no file extension or trailing slash on requested URL-path
RewriteCond $1 !(\.[^./]+)$
# And if requested URL-path plus ".html" resolves to an existing file
RewriteCond %{REQUEST_FILENAME}.html -f
# Then rewrite to add .html extension and serve that file
RewriteRule ^(.+)$ /$1.html [L]

The regular expressions used in the patterns here are described in the tutorial linked from our Charter if you'd like to look them up.

Jim