[solved] all files in folder 'folder' + delete .php from url

New to mod_rewrite? This is a good place to start.

Postby chomik » Tue Sep 01, 2009 11:31 am

It looks like everything works. So first .htaccess file in "/":
Code: Select all
Options +FollowSymLinks -MultiViews

RewriteEngine On

# Add missing trailing slashes.
# With the next rule, a request to /dir would rewrite to /folder/dir and then
# a redirect would happen to add the missing trailing slash on the directory
# and the browser would show /folder/dir/ (not /dir/).
# Check if the requested path is an existing directory in /folder.
RewriteCond %{DOCUMENT_ROOT}/folder%{REQUEST_URI}/ -d
# Only match URLs that do not end in a trailing slash. Redirect to add the slash.
RewriteRule [^/]$ %{REQUEST_URI}/ [R=301,L]

# Send all requests to /folder.
# Only match requests from outside Apache (browser's, etc.) not internal
# Apache/mod_rewrite requests (to stop loops).
RewriteCond %{ENV:REDIRECT_STATUS} ^$
# Send everything to /folder.
RewriteRule ^(.*)$ /folder/$1 [QSA,L]



Second htaccess file in "/folder":
Code: Select all
Options +FollowSymLinks -MultiViews

RewriteEngine On

# For /site/a/1/b/2 to /folder/site.php?a=1&b=2
# The PHP file must exist.
RewriteCond %{DOCUMENT_ROOT}/folder/$1.php -f
RewriteRule ^([^/]+)(?:/([^/]+)(?:/([^/]+)(?:/([^/]+)(?:/([^/]+))?)?)?)?/?$ /folder/$1.php?$2=$3&$4=$5 [L]

# Redirect to remove /index.php files and /index "files".
RewriteCond %{THE_REQUEST} \ /(.+/)?index(\.php)?(\?.*)?\  [NC]
RewriteRule ^(.+/)?index(\.php)?$ /%1 [NC,R=301,L]

# Redirect to remove .php
# Stop loops.
RewriteCond %{ENV:REDIRECT_STATUS} ^$
# Only redirect if the file exists.
RewriteCond %{SCRIPT_FILENAME} -f
#Only match PHP files. Redirect.
RewriteRule ^(.+)\.php$ /$1 [R=301,L]

# Rewrite to add .php back.
# The PHP file must exist.
RewriteCond %{SCRIPT_FILENAME}.php -f
RewriteRule ^(.*[^/])$ /folder/$1.php [QSA,L]



Thx You very much - this mod rewrite is much, much more complicated than I thought.
chomik
 
Posts: 6
Joined: Sat Aug 29, 2009 2:08 am

Previous

Return to Beginner's Corner

Who is online

Users browsing this forum: No registered users and 32 guests

cron