2

MY URL: http://localhost/test.php

I am using:

.htaccess:

RewriteEngine On 
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]

PHP:

$url = $_GET['url'];
echo var_dump($url);

But all I get for $url is:NULL NULL NULL NULL NULL NULL

Wrikken
  • 69,272
  • 8
  • 97
  • 136
Jonathan Scialpi
  • 771
  • 2
  • 11
  • 32

2 Answers2

6

Edit: adjusted to handle both the redirect and the rewrite.

RewriteEngine On 
RewriteBase /

# Redirect .php URLs to rewritten URLs
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^(.+)\.php$ $1 [L,QSA,R=301]

# Rewrite URLs for processing by router (index.php)
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L,NC]

You should exclude the RewriteCond %{REQUEST_FILENAME} !-d condition, as it will attempt to access a directory if your URL matches it. Probably not desirable when doing url rewriting.

index.php

$url = isset($_GET['url']) ? $_GET['url'] : null;
var_dump($url);
Steven Moseley
  • 15,871
  • 4
  • 39
  • 50
  • that worked dude thanks so much. weird right? Hmm im going to compare the formatting to see where i went wrong. thanks again tho, ill give u the check mark :) – Jonathan Scialpi May 15 '13 at 01:44
  • Cheers. Note my comment I just added at the end. You may want to get rid of the directory-matching condition. – Steven Moseley May 15 '13 at 01:45
  • The QSA flag may do something strange with URLs if you're passing get params – Sam Dufel May 15 '13 at 01:46
  • ahhh ok. When do you think it would be a good time to include that back? Sorry if this is a nooby question lol... and Sam, what sort of issues may arise? – Jonathan Scialpi May 15 '13 at 01:46
  • ah, maybe not, just try it and see if anything odd happens – Sam Dufel May 15 '13 at 01:47
  • @SamDufel - The QSA flag would be advisable to keep, actually. Otherwise, QueryString params will be unusable in the web app – Steven Moseley May 15 '13 at 01:48
  • 2
    @user2218297 - You probably never want to do directory matching. Here's why. Let's say you have a directory in your site called /images - if you have a request to your site like mysite.com/images it will attempt to access the images directory rather than index.php?url=images - the former is likely never what you want – Steven Moseley May 15 '13 at 01:50
  • false alarm! i thought it was working :( ... Im still getting .php after test in URL. Im going to edit the question with something I found that works. However, im reluctant to use it because it seems that there are some extras being used that might not be necessary for what I am aiming for... – Jonathan Scialpi May 15 '13 at 02:12
  • Wait a second... this does not rewrite your URLs for you, it translates rewritten URLs. Your PHP application will have to rewrite the URLs elsewhere. – Steven Moseley May 15 '13 at 02:15
  • but im getting null null null null null null, for var dump – Jonathan Scialpi May 15 '13 at 02:18
  • what is the URL you're going to? – Steven Moseley May 15 '13 at 02:22
  • starting from localhost going to: localhost/test.php – Jonathan Scialpi May 15 '13 at 02:24
  • this is working tho: http://stackoverflow.com/questions/15917258/remove-php-from-urls-with-htaccess?rq=1 – Jonathan Scialpi May 15 '13 at 02:25
  • Well that should print 'test.php' – Steven Moseley May 15 '13 at 02:25
  • That's something else. Do you want to remove php from the URLs or rewrite the URLs for translation to php? If you want both, you need to address both conditions. – Steven Moseley May 15 '13 at 02:26
  • The script I provided takes urls like `localhost/test` and invisibly rewrites them to `localhost/index.php?url=test` - isn't this what you want? – Steven Moseley May 15 '13 at 02:27
  • oh sorry if i confused you Steven. Yes I would like to remove the .php from the urls – Jonathan Scialpi May 15 '13 at 02:28
  • Note my adjustment. This will do both. – Steven Moseley May 15 '13 at 02:30
  • On a side note, you should not be rewriting .php urls using htaccess. Apache should be agnostic to what URLs are being provided. Your application should be writing the correct urls in the first place, assuming Apache knows how to translate them. – Steven Moseley May 15 '13 at 02:32
  • still didnt work :( ... i guess i can take it from here man, hate to drive you crazy lol. It seems I am going to have to examine what I am missing that exists in the link that i sent – Jonathan Scialpi May 15 '13 at 02:35
  • Riddle me this.... do you actually HAVE a file called test.php? Because that could explain why this is breaking! The point of URL-rewriting is that you're not accessing actual files, but routing requests through a central router... so in this case, you would only have one PHP file in your webroot - called index.php – Steven Moseley May 15 '13 at 02:42
  • lmao yes of course I do. that is where my content is coming from. im getting this now: http://localhost/index?url=index&url=index&url=index&url=index&url=index&url=index&url=index&url=index&url=index&url=index&url=index&url=index&url=index&url=index&url=index&url=index&url=index&url=index&url=action/box-action – Jonathan Scialpi May 15 '13 at 02:44
  • I think you need to learn how URL rewriting works. You can't just strip the .php off the end... the point of this is to route requests through a central script. – Steven Moseley May 15 '13 at 02:46
  • correct, however I want to store the URL as a variable in order to rename it to "test". Hence my var_dump($url) – Jonathan Scialpi May 15 '13 at 02:49
  • 1
    Understand something. htaccess won't redirect if the file exists in your webroot. You may want to put test.php outside your webroot, e.g. if your www is in /var/www, put test.php in a directory called /var/modules. Then have /var/www/index.php do `include("/var/modules/{$url}.php");` to load the external module – Steven Moseley May 15 '13 at 02:56
  • Steven, I think you are absolutely 100% right. That seems to make the most sense why this is not working. I really appreciate you taking the time out for this and teaching me something new! – Jonathan Scialpi May 15 '13 at 03:01
2

I just wanted to leave this somewhere for future users. This removes both HTML and PHP extensions from URLS.

#example.com/page will display the contents of example.com/page.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+)$ $1.html [L,QSA]

#301 from example.com/page.html to example.com/page
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*\.html\ HTTP/
RewriteRule ^(.*)\.html$ /$1 [R=301,L]

## don't touch /forum URIs
RewriteRule ^forums/ - [L,NC]

## hide .php extension snippet

# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L]

# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]