-2

I need help optimizing my .htaccess to automaticall redirect all pages in a certain directory to the root domain without extension.

I've several pages inside the directory www.example.com/p, I need to redirect all pages like www.example.com/p/about.html to www.example.com/about. Can this be done in a few lines in my htaccess without having to add every single page to htaccess

Thank you in advance

  • what have tried/searched? try "URL rewriting" – Pascamel Jan 14 '15 at 13:10
  • I've found codes like that `RewriteRule ^(.+?)\.html?$ /$1 [L,NC,R=301]` but I don't know how to modify this code for my situation – user3509046 Jan 14 '15 at 13:30
  • Please edit your question to add basic grammar. A sentence begins with a capital character and ends with a full stop. There is a space behind a comma and a full stop. This gives me a headache trying to read it. – Sumurai8 Jan 14 '15 at 15:05

2 Answers2

1

I was looking for a simple 301 redirect code and finally I solved the problem using that code

RedirectMatch 301 /p(.*)\.html$ //$1
  • Extra points for realizing [you didn't actually need `mod_rewrite` to do this.](http://wiki.apache.org/httpd/WhenNotToUseRewrite) – pjmorse Jan 14 '15 at 16:42
0

Add this to the .htaccess file in your website's / root directory:

Options +FollowSymLinks -MultiViews
RewriteEngine on

RewriteCond %{THE_REQUEST} \ /+p/(.*)\.html$ [NC]
RewriteRule ^ /%1 [L,R]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ /p/$1.html [L]

This rewrites all pages like www.example.com/p/about.html as www.example.com/about and then resolves the content back again from the /p directory without the URL changing in the address bar.

Ravi K Thapliyal
  • 46,997
  • 8
  • 70
  • 84
  • Thank you Ravi but I'm actually migrating content I was struggling to find an answer here finally I created this code myself and it worked `RedirectMatch 301 /p(.*)\.html$ //$1` – user3509046 Jan 14 '15 at 16:41
  • Good to know you worked it out yourself but just out of curiosity how does your website resolve content for the URLs like `www.example.com/about`? Are these directories with `index.html` files inside? – Ravi K Thapliyal Jan 14 '15 at 18:20