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

2 Answers 2

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);
Sign up to request clarification or add additional context in comments.

26 Comments

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 :)
Cheers. Note my comment I just added at the end. You may want to get rid of the directory-matching condition.
The QSA flag may do something strange with URLs if you're passing get params
@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
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
|
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]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.