Making a clean URL with php arguments in it - php

So far, I have made my .htaccess to let me remove the ".php" extension, but that isn't enough for me. I want to be so that example.com/test?id=asdfjK could be able to be accessed as example.com/asdfjK. So that it accepts only the main php get argument in the URL (I don't know what to call them.
Here is my .htaccess file so far:
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_]+)$ index.php?page=$1
RewriteRule ^([a-zA-Z0-9_]+)/$ index.php?page=$1
RewriteRule ^([a-zA-Z0-9_]+)$ /image.php?ID=$1
RewriteRule ^([a-zA-Z0-9_]+)/$ /image.php?ID=$1

There's no way to differentiate between what gets sent to index.php and what gets sent to image.php. The patterns are identical, which means everything will match the first one and nothing will get routed to image.php. You've got to add something to the url so that you can match against it. Something like:
http://example.com/image/abcdefg123456
And that means the htaccess file would look something like:
RewriteEngine On
RewriteRule ^([a-zA-Z0-9]+)/?$ index.php?page=$1 [L,QSA]
RewriteRule ^image/([a-zA-Z0-9]+)/?$ image.php?page=$1 [L,QSA]

The PHP arguments after the question mark are called the "query string".
Try something like this:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/index\.php
RewriteRule ^([a-zA-Z0-9]+)$ /index.php?page=$1 [L]
This should redirect anything from:
http://example.com/abc123
To:
http://example.com/index.php?page=abc123
See also:
htaccess rewrite for query string
.htaccess rewrite query string as path
How to prevent infinite redirect loop on htaccess?

Related

cant figure out mod_rewrite for php query string =/

here is my website
http://www.coolcodez.net/ios/nicucalc
notice when you click on pages on the nav you get urls like
http://www.coolcodez.net/ios/nicucalc/index.php?page=features
I put an .htaccess file in my nicucalc directory. I want the urls to look like this
http://www.coolcodez.net/ios/nicucalc/features
even better would be
http://www.coolcodez.net/nicucalc/features
here is my htaccess file. It's never working properly..
RewriteEngine on
RewriteCond %{QUERY_STRING} ^page=(.*)$
RewriteRule ^index\.php$ /%1/? [R=301,L]
what am i doing wrong. explanation as well please
also note: this folder is located inside of a wordpress installation folder. not sure if that htaccess file would be affecting mine somehow
The rule that you have redirects requests for index.php to /features/ (or whatever the "page" is). This is fine in and of itself but you need something that rewrites it internally back to index.php. Because of that you need 2 rules, one to redirect (matches request) and one to internally rewrite (matches URI):
RewriteEngine On
RewriteBase /ios/nicucalc/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /ios/nicucalc/index\.php\?page=([^&\ ]+)&?([^\ ]*)
RewriteRule ^ /ios/nicucalc/%1?%2 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?page=$1 [L,QSA]
The first rule matches against the request, this looks like:
GET /ios/nicucalc/index.php?page=features HTTP/1.1
The "features" is captured and backreferenced in the rule using %1. The second rule first checks if the request points to an existing file or directory. If it doesn't then the URI is captured and then rewritten to index.php and the URI gets passed to the script via the "page" parameter.

How to rewrite URL to index.php with a query even if a file/directory/location exists?

I'm trying to rewrite everything from my website to a file index.php with a query parameter that contains requested path, but when I just simply do RewriteRule ^(.*)$ index.php?url=$1 [L] my index.php file displays that url contains index.php...
Simply, I want to type localhost/123 or localhost/123.php and always get the path part in my url parameter.
print_r($_GET['url']) outputs Array ( [url] => index.php ) no matter what path part is.
Sorry for my English, hope u'll understand what I mean :)
Try using a different flag in your RewriteRule
Instead of [L]
RewriteRule ^(.*)$ index.php?url=$1 [L]
Try something like [QSA]
RewriteRule ^(.*)$ index.php?url=$1 [QSA]
For a list and information of the different types of flags, check out Apache's Documentation
http://httpd.apache.org/docs/current/rewrite/flags.html

.htaccess causing $_GET to return NULL

I am trying to pass a variable from one page to another via my url. The structure of the urls looks like this http://localhost:8888/test_portfolio?location=ignite_rockford.
Here's the relevant info from my .htaccess file:
Options -MultiViews
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ %{REQUEST_FILENAME}.php?/$1?$1=$1
</IfModule></code>
The problem seems to be with this line: RewriteRule ^(.*)$ %{REQUEST_FILENAME}.php?/$1?$1=$1, but I don't know what to do. If I add .php after test_portfolio, then it works, but otherwise when I dump $_GET['location'] I get NULL.
Oh yeah, I'm retrieving $_GET with $location = $_GET["location"];.
Any help would be appreciated.
If you are just trying to remove the .php, you can just add it and copy the query string with [QSA] tag:
RewriteRule ^(.*)$ $1.php [QSA]
Your URLs will go from:
/test to /test.php?/test?test=test are you sure this is what you want? (it looks malformed, among other things)
Usually you'll write a Router like:
RewriteRule ^(.*)$ router.php?__url=$1 [QSA]
This will give you a GET parameter __url with the original path. [QSA] will allow you to still accept all original query parameters, like location.

htaccess rewrite url from different pages

i make this in htaccess file .. for rewrite url from different page
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9_-]+)$ profile.php?username=$1 [QSA]
RewriteRule ^([a-zA-Z0-9_-]+)/$ profile.php?username=$1 [QSA]
and it's works fine ..
now i need to add more as facebook
you can put facebook.com/username
which is : facebook.com/profile.php?username=
& you can put facebook.com/pagename
which is : facebook.com/pages.php?username=
and works fine
from different pages ..
I need to make like this ..
any help ?!
RewriteRule ^([a-zA-Z0-9_-]+)$ profile.php?username=$1 [QSA]
RewriteRule ^([a-zA-Z0-9_-]+)/$ profile.php?username=$1 [QSA]
RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?username=$1 [QSA]
RewriteRule ^([a-zA-Z0-9_-]+)/$ index.php?username=$1 [QSA]
Your regex patterns match identically, so you're going to need something to differentiate between pages and people (or redirection to profile.php vs. index.php).
You can accomplish this one of two ways. First, you can go all to one page, let's call it redirect.php. On this page, you could check the id value and then redirect. So like:
RewriteRule ^([a-zA-Z0-9_-]+)/?$ redirect.php?id=$1
In redirect.php, you grab $_GET['id'] and run some query against it to determine if it's a page or person, and then redirect using something like:
header("Location: profile.php?username=".$_GET['id']);
If you strictly looking for Facebook related stuff, you can hit the graph API up instead of the database to determine if its a page or person ID. (Not sure if it can exist in both - but I think not).
If you don't have a database or data source to determine if its a page or person, then you'll need to add something to your URL strings to determine, like /pages/{pageid}, which you could then rewrite your rules to:
RewriteRule pages/([a-zA-Z0-9_-]+)/?$ index.php?username=$1 # for pages
RewriteRule ^([a-zA-Z0-9_-]+)/?$ profile.php?username=$1 # for people
If you can't use something in your URL and don't have a way to access it via database, then you're out of luck, as you're hitting two identical patterns.

Why won't this simple RewriteRule work?

This should be simple but I cannot figure it out. I have this very simple mod_rewrite rule and it just doesn't want to work. This is the entire contents of the .htaccess file.
RewriteEngine On
RewriteRule ^([A-Za-z0-9-_/\.]+)/?$ index.php?page=$1 [L]
If I call the URL domain.com/foo it should rewrite it to index.php?page=foo. But instead, it rewrites it to index.php?page=index.php. I have tried multiple URLs:
index.php?page=foo
index.php
/foo
/
In all cases, PHP acts as if 'page' is set to "index.php". It isn't a fault with index.php because I replaced the entire contents of index.php with a script that simple echoed the value of 'page' and it still comes out as index.php.
Really lost where I'm going wrong here, any help would be awesome!
Thanks
Adrian
Any reason you can't use something simpler, such as:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?page=$1 [L]
If you're trying to keep existing pages from being re-written, then the !-f will take care of that (if file does not exist and if directory does not exist, then re-write)
I think this is what your looking for. Unfortunately it doesn't really work well if the URL has other GET arguments (IE, /some/url/path?page=1).
RewriteEngine On
RewriteRule ^(.*)$ index.php?page=$1 [L]
Better idea.
RewriteEngine On
RewriteRule ^(.*)$ index.php [L,QSA]
The QSA flag will forward the GET params and then in index.php use $_SERVER['REQUEST_URI'] and parse_url to route the request.
From http://httpd.apache.org/docs/1.3/mod/mod_rewrite.html:
'qsappend|QSA' (query string append)
This flag forces the rewriting engine to append a query string part
in the substitution string to the existing one instead of replacing it.
Use this when you want to add more data to the query string via a rewrite rule.

Categories