I'd like to be able to hide a $_REQUEST variable name in a visitor's address bar, and also automatically append the variable name to all data requests, but only when the .php extension is not included.
Currently requests look like this:
example.com/?page=request
I'd like them to look like this
example.com/request
The problem is, domains like this still need to work:
example.com/mail.php
So I figure I'd like all requests to files that don't end in the extension .php to invisibly forward to the contents of
example.com/?page=*
While actually displaying the address:
example.com/*
Here's what I have so far:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^page=-
RewriteRule ^(.*)$ /$1? [L,R]
But this doesn't even replace the string when it's entered.
I wouldn't mind actually having to add the name of each accessible .php file to the .htaccess file, this would probably build on security.
You probably want to forward /request URI to /?page=request. If thats the case use this rule:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^].]+)$ /?page=$1? [L,QSA]
Related
I have following URLs:
http://www.example.com/item?title=titlename&id=5 and
http://www.example.com/page?title=titlename
I want to convert them like:
http://www.example.com/item/titlename/5
http://www.example.com/page/titlename
Note: the page and item are files (ie: item.php and page.php), I was able to remove the .php extension using the following code:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [NC,L]
Any little help would be appreciated.
i just want to internally rewrite the url from something like http://www.example.com/item?title=titlename&id=5 and http://www.example.com/page?title=titlename to something like this: http://www.example.com/item/titlename/5 or http://www.example.com/page/titlename.
That should be the other way round... internally rewrite from http://www.example.com/item/titlename/5 to http://www.example.com/item?title=titlename&id=5. And presumably you should be including the .php extension on the target URL? (Don't rely on another directive to do append the file extension.)
Try something like the following:
Options -MultiViews
RewriteEngine On
RewriteRule ^([a-z]+)/([\w-]+)(?:/(\d+))?$ /$1.php?title=$2&id=$3 [L]
This will handle a URL both with and without the trailing ID (eg. /5). However, if you specify a URL of the form /item/titlename (ie. no numeric id), then you will naturally get an empty id URL parameter passed to your script.
Note that MultiViews must be disabled for this to work, otherwise mod_negotiation will rewrite the URL from item to item.php (for example) before mod_rewrite and you won't get the URL parameters passed.
If you specifically need to check that the target file, eg. page.php exists before rewriting then you can include an additional condition to check this. However, I wouldn't have thought this was necessary since you'll get a 404 regardless and checking that the file exists is relatively expensive.
Options -MultiViews
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^([a-z]+)/([\w-]+)(?:/(\d+))?$ /$1.php?title=$2&id=$3 [L]
This assumes that your files exist in the document root of your site.
Just to clarify, as mentioned in comments, you should already be linking to URLs of the form http://www.example.com/item/titlename/5 in your application.
Please everyone, I am trying to prevent direct access to all files and pages in my application, and also to ensure that no one can determine what particular language the site is written in.
So, I want a situation where calls to myapp.com/path/to/page.php, whether it exists or not, internally redirects to myapp.com/page_not_found.php. And similarly, myapp.com/path/to/anywhere redirects to myapp.com/index.php.
I have been able to handle the later using
If the requested path and file doesn't directly match a physical file
RewriteCond %{REQUEST_FILENAME} !-f
and the requested path and file doesn't directly match a physical folder
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)(.*)$ index.php?path=$1&sub=$2 [QSA,L]
But for the former, I don't have any idea where to begin.
To redirect *.php to /page_not_found.php:
RewriteCond %{REQUEST_URI} .*\.(php)
RewriteRule ^(.*)/ /page_not_found.php [R]
If you want to redirect other file types, like say *.asp, change (php) to (php|asp).
To redirect everything else, add this too:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [L]
As a note here, you don't need to pass in the path as a get parameter (i.e. index.php?path=$1...). Instead, from within PHP, read the $_SERVER['REQUEST_URI'] value instead. This stops your original query string from getting messed up (i.e. you can use $_GET as normal). So for example if the original request is myapp.com/path/to/page?id=1 then $_GET['id'] will continue to work as normal and $_SERVER['REQUEST_URI'] is /path/to/page?id=1.
Note that $_SERVER['REQUEST_URI'] can contain a query string so you'll want to split at ? to chop that off:
// Grab the path:
$path=$_SERVER['REQUEST_URI'];
// Split at ? to remove the query string if there is one:
$path=explode('?',$path,2);
// Get the first part only:
$path=$path[0];
I have domain arranged like this
localhost/editor/index.php // This is the php file which require get variables
localhost/editor/.htaccess // htaccess file which will pass values to index.php
HTAccess file : code
RewriteEngine on
RewriteCond %{REQUEST_URI} (.*)/(.*)/
RewriteRule (.*)/(.*)/ index.php?u=$1&e=$2
I want to make url like this
localhost/editor/variable1/variable2/
after this if someone will miss a slash at end he will get object not found error
I have tried different ways but i am unable to fix this.
You can use this code instead
RewriteEngine On
RewriteBase /editor/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?u=$1&e=$2 [L]
This way, you can access
localhost/editor/variable1/variable2
or
localhost/editor/variable1/variable2/
But i recommend you to choose one and to disallow the other one (to avoid duplicate content)
I was wondering if I'm supposed to use .htaccess to change a get variable to a value from the database (username).
so if there is
http://www.url.com/user.php?u=1
how do you conver it to
http://www.url.com/andrewliu
Thanks!
You need to change your user.php to take a username instead of a userid. Then you can use something like:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/?([a-z0-9_-]+)/?$ /user.php?u=$1 [L]
This passes the username through the u query string parameter, essentially: /user.php?u=andrewliu
Otherwise there's no way htaccess and mod_rewrite can know what the mapping is between user_id and username. Alternatively, you can write a database script and use RewriteMap to create a mapping for you:
RewriteMap usermap prg:/path/to/userscript
and in your htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/?([a-z0-9_-]+)/?$ /user.php?u=${usermap:$1} [L]
The last option, if you've only got, say, 5 users (or some small amount), you can make explicit rewrites:
RewriteEngine On
RewriteRule ^/?andrewliu$ /user.php?u=1 [L]
RewriteRule ^/?anotheruser$ /user.php?u=2 [L]
RewriteRule ^/?foousername$ /user.php?u=3 [L]
You need to use Mod-ReWrite. You can use this in your htaccess files but if you have access to your httpd.conf file it will prove to be quicker there.
You would ideally use the ?u=1 in your htaccess file and use that to find the the name and append the name onto the end. Otherwise you will have to search for the username and could be succesiptable to spelling mistakes etc etc.
RewriteRule ^/([0-9]+)/.*$ /user.php?u=$1 [L]
This is how Stackoverflow does it, try accessing this page without the number in the url and you will get a 404 page not found. Get the number right and what ever else you type after the forward slash will be exchanged for the correct words! This is much more convienent!
Use $GET in php to get the value of 'u'
Then use your logic to convert it to name.
Now just use redirect function to go to to that URL
The main navigation of my site is coded like this:
<li>'.$value.'</li>'."\n";
But I would like the URL of the links to look like domain.co.nz/pagename, not domain.co.nz/index.php?pageId=pagename
How would you recommend I accomplish this?
Something like this should work:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?pageID=$1
First line turns on mod_rewrite. Second line sets the base URL to / (it's annoying, but you have to set it to the base path you're dealing with). Third and fourth lines make sure the request doesn't exist as a file, or as a directory. And the last line is the actual magic; basically it searches for "anything", captures what it finds in $1, and "rewrites" the URL to index.php?pageID=$1. If you learn to use regexes, you can do much more complicated things as well.
Yes, you can accomplish this with a .htaccess RewriteRule. In your .htaccess file, include:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) index.php?pageId=$1
This means:
If the REQUEST_FILENAME is not a valid file, redirect the entire URL to index.php?pageId=the entire URL
You'd then change your navigation to:
echo "<li>'.$value.'</li>'."\n";
Edit: I moved Trivikrtam's edit inline, see above. The RewriteRule should be index.php?pageId=$1 not /index.php?pageId=$1. Thanks #Trivikrtam!