sgledhill
sgledhill

Reputation: 75

remove folder from url with htaccess

So I have a folder named pages which includes the pages for my site, when opening one of these files the url reads -

zebrafishweb/pages/contact

I would like it to just read

zebrafishweb/contact

I already have this htaccess file to remove any .php .html extensions which i would also like to carry on using. I don't have a clue how to write these htaccess files just copied it from the web and tailored it to me!

 RewriteEngine on

 RewriteRule ^(themes|order-now|submissions|faq|contact)$ $1.php [NC,L]
 RewriteRule ^(home|faq)$ $1.html [NC,L]

Upvotes: 1

Views: 53

Answers (1)

anubhava
anubhava

Reputation: 784898

  1. You already have a .htaccess inside /pages/ subdirectory with some rules for .php and .html extensions
  2. You want a redirect to shorter URI /contact instead of /pages/contact

With those you can have your rules like this:

site root .htaccess:

RewriteEngine On

RewriteRule ^contact/?$ pages/contact.php [L,NC]

pages/.htaccess:

RewriteEngine On

# redirect to shorter URL
RewriteCond %{THE_REQUEST} /pages(/contact)[?\s]
RewriteRule ^ %1 [L,R=302]

# add .php to these files
RewriteRule ^(themes|order-now|submissions|faq|contact)/?$ $1.php [NC,L]

# add .html to these files
RewriteRule ^(home|faq)/?$ $1.html [NC,L]

Upvotes: 1

Related Questions