need help with mod_rewrite!

Discussions about Apache mod Rewrite, .htaccess, Use, experiences ... URL Rewriting.

Moderator: Moderators

need help with mod_rewrite!

Postby SoonDead » Thu Mar 11, 2010 3:20 pm

I have a site and a message board, and the message board already uses the phpbb premod.
the main site has pages like
example.com/index.php
example.com/something.php
example.com/other_thing.php
example.com/something_else.php
they are all php-s.

the message board is located under /forums/ but the .htaccess is in the root directory.

What I want is to write a few lines in the .htaccess to rewrite the php-s in the root directory (and only in the root directory!) to look like a directory, like example.com/something/ etc...
I don't want it to redirect index.php to example.com/index/ but to simply example.com/
All the other php-s should look like directories in the root.
It shouldn't interfere with content in subdirectories as there are a lot of them and there are different sites under them.

Also the php-s are indexed by google (with a decent ranking) so it would be preferrable to 301 redirect them rather then losing their ranking.

Can somebody help me to write this rewriterule? I'm not so experienced with mod_rewrite and a few of you are quite experts on the subject, so I'm asking for help.
User avatar
SoonDead
 
Posts: 32
Joined: Wed May 20, 2009 8:39 pm

Advertisement

Re: need help with mod_rewrite!

Postby dcz » Fri Mar 12, 2010 1:50 pm

If you r forum is in a sub directory, and if you're not using virtual root, then you should use the "more options" option of the .htaccess generator, and then alternate location to generate an .htaccess meant to be used in that sub dir. It's the easiest way not to mess up with rewriterules from the root directory's .htaccess.

Then, to http 301 redirect example.com/anyfile.php to example.com/anyfile/ you can use :

Code: Select all
RewriteRule ^([a-z0-9_-]+)\.php$ $1/ [QSA,L,NC,R=301]

If you have only few files, specifying their names is a good way to prevent unwanted redirect :
Code: Select all
RewriteRule ^(file1|file2|file3|file4|etc)\.php$ $1/ [QSA,L,NC,R=301]

Or if you prefer to only keep few files un redirected (like index.php), you can use :
(?!galerie)
Code: Select all
RewriteRule ^(?!index|other_file_name)([a-z0-9_-]+)\.php$ $1/ [QSA,L,NC,R=301]


For the index.php redirect, you could use :
Code: Select all
RewriteRule ^index\.php$ / [QSA,L,NC,R=301]


after this you would need to allow file_name/ to hit file_name.php script :
Code: Select all
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z0-9_-]+)/$ $1.php [QSA,L,NC]

The two RewriteCond here are to prevent messing with physical directories and files, a true directory should show up and not be linked to a php script.

This being said, note that this will mean that you will be creating virtual directories, which can break all relative paths in href links. You can use a base href tag to fix relative href, or just use full urls with domain in all links.
Then, this will of course not rewrite the links themselves in your pages, it will only do the redirect and make them work.
Also, it is possible that with your server, some extra slashes are required in the rewriterules, but they should work.

++
Useful links :
SEO Forum || SEO Directory || SEO phpBB || Search
____________________

Liens Utiles :
Forum référencement || Annuaire référencement || Référencement phpBB || Recherche
dcz
Admin
Admin
 
Posts: 21219
Joined: Fri Apr 28, 2006 9:03 pm

Re: need help with mod_rewrite!

Postby SoonDead » Fri Mar 12, 2010 3:35 pm

Thanks for the reply it was very educational!

Well I still would like to have the "phpBB SEO generated" .htaccess to be in the root directory, as I want to have my sitemap in the root directory (I moved the sitemap.php there and I want it to be rewritten to xml), so I would rather append the new lines to that .htaccess.

Since I have only a few files to redirect, I will try this syntax as it looks like it does the least collateral.
Code: Select all
RewriteRule ^(file1|file2|file3|file4|etc)\.php$ $1/ [QSA,L,NC,R=301]


Also this is great for the index.
Code: Select all
RewriteRule ^index\.php$ / [QSA,L,NC,R=301]


And I need this to actually run the php-s
Code: Select all
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z0-9_-]+)/$ $1.php [QSA,L,NC]


Well if I want to append this to the generated htaccess, then where should I append it (after the rewriteengine on of course, but does it matter)? Should I do anything differently?
User avatar
SoonDead
 
Posts: 32
Joined: Wed May 20, 2009 8:39 pm

Re: need help with mod_rewrite!

Postby SoonDead » Tue Mar 16, 2010 7:37 pm

I'm sill waiting for some approvation as I don't want it to mess up something.
User avatar
SoonDead
 
Posts: 32
Joined: Wed May 20, 2009 8:39 pm

Re: need help with mod_rewrite!

Postby SoonDead » Tue Mar 23, 2010 1:52 pm

It does not work!

It throws "the page is not properly redirected" error in firefox.

also the part "RewriteRule ^index\.php$ / [QSA,L,NC,R=301]" ends up in an endless loop.

Can someone help me with it again?
User avatar
SoonDead
 
Posts: 32
Joined: Wed May 20, 2009 8:39 pm


Return to Apache mod Rewrite

 


  • Related topics
    Replies
    Views
    Last post

Who is online

Users browsing this forum: No registered users and 3 guests