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

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

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

Postby chomik » Sat Aug 29, 2009 2:24 am

I have all files in folder 'folder' I would like delete 'folder' from url and delete '.php' from url:

Code: Select all
Options FollowSymLinks
RewriteEngine on

/*
if url is empty, for example: http://localhost/ show site: http://localhost/folder/index.php:
*/
RewriteRule ^$ folder/index.php [L]
/*
else if url has more than 1 char, for example: http://localhost/site show site: http://localhost/folder/site.php:
*/
RewriteRule ^(.+)$ folder/$1.php [L]



And it doesn't work - why ?
Last edited by chomik on Tue Sep 01, 2009 12:06 pm, edited 1 time in total.
chomik
 
Posts: 6
Joined: Sat Aug 29, 2009 2:08 am

Postby richardk » Sat Aug 29, 2009 2:44 pm

Try
Code: Select all
Options +FollowSymLinks

RewriteEngine On

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

# Redirect to remove .php
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{DOCUMENT_ROOT}/folder%{REQUEST_URI} -f
RewriteRule ^(.+)\.php$ /$1 [R=301,L]

# Rewrite to add .php back.
RewriteCond %{DOCUMENT_ROOT}/folder%{REQUEST_URI}.php -f
RewriteRule ^(.*[^/])$ /$1.php [QSA,L]


And it doesn't work - why ?

The last rule also matches the internal request to /folder/site.php.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby chomik » Sun Aug 30, 2009 12:50 am

:o

It doesn't work. Your rules are so complicate that I don't know what I can change - lol
chomik
 
Posts: 6
Joined: Sat Aug 29, 2009 2:08 am

Postby richardk » Sun Aug 30, 2009 11:14 am

What did you do?
What happened when you went to /site?

Try replacing
Code: Select all
/$1.php

with
Code: Select all
/folder/$1.php

and replacing both
Code: Select all
index\.php

with
Code: Select all
index(\.php)?
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby chomik » Mon Aug 31, 2009 1:38 am

thx replacing
Code: Select all
/$1.php

with
Code: Select all
/folder/$1.php

http://localhost/site works but http://localhost/ doesn't work


replacing
Code: Select all
index\.php

with
Code: Select all
index(\.php)?

no result so I deleted it.


As I said before - http://localhost/ didn't work so I have added my own code to Your very, very complicate code ( :) ):
Code: Select all
# For http://localhost/
RewriteRule ^$ folder/index.php [L]

and http://localhost/ works now. (I am not sure if this code is ok so correct me if something is wrong)


I would like to use also for example address
<a href="http://localhost/site/a/1/b/2">site</a>
like
<a href="http://localhost/folder/site.php?a=1&b=2">site</a>
so I have added:
Code: Select all
# For http://localhost/site/a/1/b/2
RewriteRule ^(.+)/(.*)/(.*)/(.*)/(.*)$ /folder/$1.php?$2=$3&$4=$5 [L]


it works for:
<a href="http://localhost/site/a/1/b/2">site</a>
but not for:
<a href="http://localhost/site/a/1">site</a>
Why ?

So I have got now:
Code: Select all
Options +FollowSymLinks

RewriteEngine On

# For http://localhost/ - this is my own code:
RewriteRule ^$ folder/index.php [L]

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

# Redirect to remove .php
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{DOCUMENT_ROOT}/folder%{REQUEST_URI} -f
RewriteRule ^(.+)\.php$ /$1 [R=301,L]

# Rewrite to add .php back.
RewriteCond %{DOCUMENT_ROOT}/folder%{REQUEST_URI}.php -f
RewriteRule ^(.*[^/])$ /folder/$1.php [QSA,L]

# For http://localhost/site/a/1/b/2 - this is my own code:
RewriteRule ^(.+)/(.*)/(.*)/(.*)/(.*)$ /folder/$1.php?$2=$3&$4=$5 [L]



What is wrong ? Why I can use http://localhost/site/a/1/b/2 but not http://localhost/site/a/1 ?
chomik
 
Posts: 6
Joined: Sat Aug 29, 2009 2:08 am

Postby richardk » Mon Aug 31, 2009 8:28 am

Let's try it a different way.

In /.htaccess
Code: Select all
Options +FollowSymLinks

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]


Then in /folder/.htaccess
Code: Select all
Options +FollowSymLinks

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]

(This is based on this FAQ post.)
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby chomik » Mon Aug 31, 2009 10:38 am

Everything works ( even if I place everything in one file in / ) without 1 thing. :P

When I have for example: http://localhost/site/a/1/b/2 or http://localhost/index/a/1/b/2 and in php:
Code: Select all
<?php
if(isset($_GET["a"]) && $_GET["a"]==1)
{
   echo "exists a = 1";
}
else
{
   echo "not exist";
}
?>


I see "not exist". Do You know why ?
chomik
 
Posts: 6
Joined: Sat Aug 29, 2009 2:08 am

Postby richardk » Mon Aug 31, 2009 10:50 am

What is the value of $_SERVER['QUERY_STRING']?
What output do you get if you print_r() the $_GET array?
Code: Select all
<?php

echo 'Query string: ' . $_SERVER['QUERY_STRING'] . "\n\nGET ";
print_r($_GET);
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby chomik » Tue Sep 01, 2009 2:02 am

Now I have got:
Code: Select all
Query string: GET Array ( )


When I had file:
Code: Select all
Options +FollowSymLinks

RewriteEngine On

# For http://localhost/ - this is my own code:
RewriteRule ^$ folder/index.php [L]

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

# Redirect to remove .php
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{DOCUMENT_ROOT}/folder%{REQUEST_URI} -f
RewriteRule ^(.+)\.php$ /$1 [R=301,L]

# Rewrite to add .php back.
RewriteCond %{DOCUMENT_ROOT}/folder%{REQUEST_URI}.php -f
RewriteRule ^(.*[^/])$ /folder/$1.php [QSA,L]

# For http://localhost/site/a/1/b/2 - this is my own code:
RewriteRule ^(.+)/(.*)/(.*)/(.*)/(.*)$ /folder/$1.php?$2=$3&$4=$5 [L]


I had:
Code: Select all
Query string: a=1&b=2 GET Array ( [a] => 1 [b] => 2 )


Now when I use
Code: Select all
RewriteRule ^(.+)/(.*)/(.*)/(.*)/(.*)$ /folder/$1.php?$2=$3&$4=$5 [L]

instead of
Code: Select all
RewriteRule ^([^/]+)(?:/([^/]+)(?:/([^/]+)(?:/([^/]+)(?:/([^/]+))?)?)?)?/?$ /folder/$1.php?$2=$3&$4=$5 [L]


$_GET still doesn't work - why ?
chomik
 
Posts: 6
Joined: Sat Aug 29, 2009 2:08 am

Postby richardk » Tue Sep 01, 2009 9:56 am

MultiViews (more) is most likely the problem.

Replace both
Code: Select all
Options +FollowSymLinks

with
Code: Select all
Options +FollowSymLinks -MultiViews


Also replace the
Code: Select all
[L]

with
Code: Select all
[QSA,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Next

Return to Beginner's Corner

Who is online

Users browsing this forum: No registered users and 25 guests

cron