NULL
NULL

Reputation: 1589

mod_rewrite hide file extensions

Options +FollowSymLinks 
RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) /files.php?q=$1

RewriteCond %{HTTP_HOST} ^mysite.com
RewriteRule (.*) http://m.mysite.com/$1 [R=301,L]

AddHandler application/x-httpd-php5 .html .htm .txt .php

my .htaccess uses the main "files.php" for all requests. in my files.php i have some code in it that includes hrefs which looks like this:

/people/john --> when a person clicks on this, it should go to: people.php?q=john however, my .htaccess is not redirecting when person clicks on this /people/john. btw, the url should always say /people/john NOT people.php?q=john even though that is what is happening behind the scenes.

Just wanted to say, i have

/city/newyork --> city.php?q=newyork
/country/australia --> country.php?q=australia 

I have been struggling and searching here to find exactly, but still no luck. i appreciate all the responses.

Upvotes: 0

Views: 401

Answers (4)

NULL
NULL

Reputation: 1589

Faa, I did what you said, and below is my server variables, i think the correct variables should be the one that says "should be this:" below.

["QUERY_STRING"]=>
  string(17) "q=city/newyork"
  ["REDIRECT_QUERY_STRING"]=>
  string(17) "q=city/newyork"
  ["REDIRECT_STATUS"]=>
  string(3) "200"
  ["REDIRECT_URL"]=>
  string(16) "/city/newyork"
["REQUEST_URI"]=>
  string(16) "/city/newyork"
  ["SCRIPT_FILENAME"]=>
  string(30) "/home/mywebsite/myfiles.php"
  ["SCRIPT_NAME"]=>
  string(14) "/myfiles.php"
["PHP_SELF"]=>
  string(14) "/myfiles.php"
  ["argv"]=>
  array(1) {
    [0]=>
    string(17) "q=city/newyork"
  }




should be this:
["QUERY_STRING"]=>
  string(16) "q=newyork"
  ["REDIRECT_QUERY_STRING"]=>
  string(16) "q=newyork"
  ["REDIRECT_STATUS"]=>
  string(3) "200"
  ["REDIRECT_URL"]=>
  string(25) "/city/newyork"
["REQUEST_URI"]=>
  string(25) "/city/newyork"
  ["SCRIPT_FILENAME"]=>
  string(30) "/home/mywebsite/city.php"
  ["SCRIPT_NAME"]=>
  string(14) "/city.php"
["PHP_SELF"]=>
  string(14) "/city.php"
  ["argv"]=>
  array(1) {
    [0]=>
    string(16) "q=newyork"
  }

Upvotes: 0

Jeroen Moors
Jeroen Moors

Reputation: 981

Based on your examples, you've got url's like this:

/city/newyork, where city is the php file to call with parameter newyork

If that's what you want, try this: Options +FollowSymLinks RewriteEngine on

# Rewrite /city/newyork to city.php?q=newyork
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/(.*) /$1.php?q=$2 [L]

# Rewrite all urls without a second / to files.php?q=$1
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) /files.php?q=$1 [L]

RewriteCond %{HTTP_HOST} ^mysite.com
RewriteRule (.*) http://m.mysite.com/$1 [R=301,L]

AddHandler application/x-httpd-php5 .html .htm .txt .php

Upvotes: 1

Do you want to make it dynamic? I mean, /$anything1/$anything2 will be remapped into /$anything1.php?=$anything2:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/([a-z0-9-_]+)/([a-z0-9-_]+)/?$
RewriteRule ^(.*) /%1.php?q=%2

Or try this if the code above doesn't work:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/([a-z0-9-_]+)/([a-z0-9-_]+)/?$
RewriteRule ^(.*) http://%{HTTP_HOST}/%1.php?q=%2 [P]

Upvotes: 0

Here is what I use with CodeIgniter (supports very deep a/b/c/d/e/f/g)

    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ files.php?/$1 [L]

Essentially line by line it says
- switch rewrite engine on
- use the web_root (so it may not be / for you if you are in a sub-folder)
- if the url is not an existing file
- and the url is not an existing dir
- map the argument to files.php

Just realized you are using Files.php... strange, should just call it index for consistency if it is what everything should route through, or make an index that routes folder/files/* through to index.php?tab=files&...

Hope this helps

Upvotes: 0

Related Questions