short URL through htaccess file in php - php

I have a website http://sharenotes.xyz/.
In this website users can save and share quick notes to others users.
There is a unique note id for each note. (id can only contain [0-9A-Za-z_] charachters).
Unique note id is present in the url http://sharenotes.xyz/hithere.
In this case hithere is the unique note id.
In actual the url is like
http://sharenotes.xyz/index.php?id=hithere.
My folder structure looks like -
and index.php file is present in public folder.
What will be the content of the .htaccess file to short the url from http://sharenotes.xyz/index.php?id=hithere to http://sharenotes.xyz/hithere and in which folder should I place that .htaccess file ?
I know php but I am new in htaccess file (stored in public_html folder).
UPDATE
I was forget to tell you something that -
There is folder named as public which servers all user accessible files.
So I have also hide the name public from the url throught .htaccess file.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ public/ [L]
RewriteRule (.*) public/$1 [L]
</IfModule>
Options -Indexes
That's why you wouldn't see public in url.

This would be the required rewriting rule:
RewriteEngine on
RewriteRule ^/?([0-9a-zA-Z_]+)/?$ /public/index.php?id=$1 [END]
Best is to implement such rules in the actual http server's host configuration. Only if you do not have access to that, then you should use a distributed configuration file, often called ".htaccess". But that comes with a number of disadvantages. If you decide to use one, then place it inside the top folder of your hosts DOCUMENT_ROOT, so here inside the "public_html" folder.
Obviously the rewriting modules needs to be loaded into the apache http server for this. And if you are using a distributed configuration file then you also need to enable the interpretation of such files for that location (read about the AllowOverride directive in the documentation of the http server).
Most likely you will need to add further rewriting rules to sort out requests to other resources.
UPDATE
Considering your comments and the additional information you now added to describe your actual situation this variant probably is close to what you are looking for:
RewriteEngine on
RewriteCond /public%{REQUEST_URI} -f [OR]
RewriteCond /public%{REQUEST_URI} -d
RewriteRule ^ /public%{REQUEST_URI} [END]
RewriteRule ^/?([0-9a-zA-Z_]+)/?$ /public/index.php?id=$1 [END]

Related

.htaccess link with parameters

I have a page with URL like this:
https://something.com/paste/log.php?log=H7nSEIPaVr
(By the way my homepage is:
https://something.com/paste/index.php
)
I want to make it work by just giving the log parameter like this:
https://something.com/paste/H7nSEIPaVr
And it would redirect me to the original url.
(or it would be better if it just give me the paramter - ?log=xxx)
I have a .htaccess file, but it is not working for me.
(rewrite is enabled, so the htaccess is working well)
RewriteEngine on
RewriteRule /paste/^([a-zA-Z0-9]*$)/? /log.php?log=$1 [QSA]
This probably is the rule you are looking for:
RewriteEngine on
RewriteRule ^/?paste/([a-zA-Z0-9]+)/?$ /paste/log.php?log=$1 [L]
That rule needs to be implemented in the central http server's host configuration. Or, if you do not have access to that, you can instead use a distributed configuration file (typically called ".htaccess") which has to be located inside the http server's DOCUMENT_ROOT folder.
An alternative would be to place that variant in a distributed file inside the /paste folder:
RewriteEngine on
RewriteRule ^([a-zA-Z0-9]+)/?$ log.php?log=$1 [L]

Send all Request to Inside folder in php?

sharedHostingFolder
folder1
folder2
folder3
xampp htdocs folder
Folder Structure:
root/sharedHostingFolder
-need an .htaccess file here
-folder1
--index.php
--dude.php
-folder2
--hello.php
--index.php
---folder5
----index.php
-folder3
--hi.php
--index.php
Request Structure
Request "root/sharedHostingFolder/hello.php" will show file inside "root/sharedHostingFolder/folder2/hello.php"
Request "root/sharedHostingFolder/" will show file inside "root/sharedHostingFolder/folder2/index.php"
Request "root/sharedHostingFolder/folder1/dude.php" will show file inside "root/sharedHostingFolder/folder1/dude.php"
Request "root/sharedHostingFolder/folder1/" will show file inside "root/sharedHostingFolder/folder1/index.php"
Request "root/sharedHostingFolder/folder2/hello.php" will show file inside "root/sharedHostingFolder/folder2/hello.php"
Request "root/sharedHostingFolder/folder2/" will show file inside "root/sharedHostingFolder/folder2/index.php"
Request "root/sharedHostingFolder/folder3/hi.php" will show file inside "root/sharedHostingFolder/folder3/hi.php"
Request "root/sharedHostingFolder/folder5/" will show file inside "root/sharedHostingFolder/folder2/folder5/index.php"
I HAVE NO ACCESS TO php.ini file
.htaccess file
RewriteEngine on
DirectoryIndex index.php
RewriteCond %{REQUEST_URI} ^/folder1/ [OR]
RewriteCond %{REQUEST_URI} ^/folder2/ [OR]
RewriteCond %{REQUEST_URI} ^/folder3/
RewriteRule - [END]
RewriteRule ^ /folder2%{REQUEST_URI} [END]
httpd.conf file:
<Directory />
AllowOverride All
Order allow,deny
Allow from all
</Directory>
Error:
object not found
I researched and found out .htaccess will do that easily . But, Really I don't know how to do that.
This is a strange question. TO me it reads a bit like a classical xy problem... As if you did not ask how to solve your actual task, but how to get something to work that you think might solve your task...
Anyway, looking at your examples I have the impression that little is to be done, mostly some exceptions that need to be implemented as rewriting rules. So something like that:
RewriteEngine on
DirectoryIndex index.php
RewriteCond %{REQUEST_URI} ^/sharedHostingFolder/folder1/ [OR]
RewriteCond %{REQUEST_URI} ^/sharedHostingFolder/folder2/ [OR]
RewriteCond %{REQUEST_URI} ^/sharedHostingFolder/folder3/
RewriteRule - [END]
RewriteRule ^ /sharedHostingFolder/folder2%{REQUEST_URI} [END]
This leaves requests to URIs starting with one of the folder names untouched, replying on classical file system mapping and defining the index.php script as fallback logic for those folders. And it maps everything else into folder2.
It does not get clear from your question what that "www" string is meant to represent. If that is meant to represent another folder level then you might have to add that level to the rules above.
For this to work the rewriting module needs to be enabled, obviously. Best is to implement such rules in the http server's host configuration. If you do not have access to that you can also use a distributed configuration file (".htaccess"), but you need to enable the interpretation of such file for the requested location (see the apache documentation about the AllowOverride directive).

HTACCESS: URI rewrite everything to index.php (Within the same directory)

I have a uri: www.domain.com/leasing/properties/
Within that directory, I created a file .htaccess:
RewriteEngine on
RewriteRule ^(.*)$ index.php?uri=$1 [L,QSA]
What I want, is that regardless of that users type after properties/ (Ej. www.domain.com/leasing/properties/somethigelse/I-do-not.care/what##$%they\type) to be handled by www.domain.com/leasing/properties/index.php
I do not need structure or anything else. index.php will grab $_REQUEST['uri'] and check against a database. So, again, I do not care what's typed after www.domain.com/leasing/properties/ so long index.php can get the URI and handle an action according to $_REQUEST['uri']
I want the address bar to not not change. Whatever a user types, to remain the same.
The current file is giving me a 404 error.
PS. I know close to nothing about Apache .htaccess
Create a .htaccess file with the following:
RewriteEngine On
RewriteRule ^([^/]+)(.*)$ /leasing/properties/ [R]
And place it within /leasing/properties/ folder.
RewriteEngine on
RewriteRule ^/leasing/properties/(.*)$ /leasing/properties/index.php [L,QSA]
If that does not work, your Apache server may not be configured to allow .htaccess files.

.htaccess for site in sub-directory using Yii framework

I have looked at several examples of htaccess configs for websites within sub-directories, and tried most of them without 100% success.
My setup is:
using Yii framework
htaccess at public_html/.htaccess
site located inside public_html/mysite directory
index handling all requests located at public_html/mysite/frontend/www/index.php
The status of the URLs:
www.mysite.com works fine [ok]
www.mysite.com/controller/action shows me the homepage [wrong]
www.mysite.com/mysite/frontend/www/controller/action works fine [wrong, the item above should work instead]
My .htaccess at the moment looks like this:
AddHandler application/x-httpd-php53s .php .html
Options +SymLinksIfOwnerMatch
IndexIgnore */*
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} ^(www.)?mysite.com$
RewriteCond %{REQUEST_URI} !^/mysite/frontend/www
RewriteRule ^(.*)?$ /mysite/frontend/www/index.php [L]
I have tried everything, but I have no idea why www.mysite.com/controller/action won't work :(
Any help would be really appreciated! Thanks!
I found the answer to this similar question to be helpful. Here is how my rewrite rules ended up:
#Forward all non-existent files/directories to Yii
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) subdir/index.php/$1 [QSA,L]
This takes all non-existent files/folders and sends them to the yii script with initial url appended. QSA appends any query string that may be present in the initial url.
You didn't mention if you configured Yii's Url Manager for clean URLs. You need to, otherwise Yii expects the "route" to appear as a GET param named "r". If you didn't, consult this section of the definitive guide
You dont need to edit .htaccess. You just need to move the Yii entry script (index.php) and the default .htaccess up from the subdirectory to the webroot (so that they reside directly under public_html). Once you move index.php and .htaccess to the root directory, all web requests will be routed directly to index.php (rather than to the subdirectory), thus eliminating the /subdirectory part of the url.
After you move the files, you will need to edit index.php to update the references to the yii.php file (under the Yii framework directory) as well as the Yii config file (main.php). Lastly, you will need to move the assets directory to directly the webroot, since by default, Yii expects the assets directory to be located in the same location as the entry script).
That should be all you need to do, but if you need more details, I describe the approach fully here:
http://muhammadatt.tumblr.com/post/83149364519/modifying-a-yii-application-to-run-from-a-subdirectory
I also didn't update the .htaccess file, easier to modify the httpd.conf virtual host for the subdomain and change the DocumentRoot to point to your yii folder.

htaccess rewrite ".../pages/about.php" to ".../about"

I've searched and found a lot of questions on this site and elsewhere that are very similar, but I've tried implementing and modifying all the suggestions I've found and none of it works. I realize this is a very basic question an I am extremely frustrated because nothing I'm trying is working.
With that having been said... I am trying to organize my content pages within kurtiskronk.com/pages/... (e.g. kurtiskronk.com/pages/about.php)
What I want to do is make it so that I can simply link to kurtiskronk.com/about ... So how do I go about stripping "pages/" and ".php"? I don't have a ton of content pages, so it's not a big deal if I have to specify for each page, though something dynamic would be handy.
NOTES: I am using Rackspace Cloud hosting, and WordPress is installed in /blog. My phpinfo() can be seen at http://kurtiskronk.com/pages/phpinfo.php
This is my existing .htaccess file (in the root)
php_value register_globals "on"
Options +FollowSymLinks
RewriteEngine On
#301 redirect to domain without 'www.'
RewriteCond %{HTTP_HOST} ^www\.kurtiskronk\.com$ [NC]
RewriteRule ^(.*)$ http://kurtiskronk.com/$1 [R=301,NC]
RewriteBase /
RewriteCond %{ENV:PHP_DOCUMENT_ROOT}/pages/$1 -f
RewriteRule ^(.*)$ pages/$1 [L]
RewriteCond %{ENV:PHP_DOCUMENT_ROOT}/pages/$1.php -f
RewriteRule ^(.*)$ pages/$1.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^blog/ blog/index.php [L]
# PHP - MAIL
php_value mail.force_extra_parameters -kurtis#kurtiskronk.com
I tested and the rewrite works with the line below (/about as URL brings up file /pages/about.php), but then the homepage gives a 500 Internal Server Error:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /pages/$1.php [L]
So I'm still sort of in the same boat as before, and as a follow-up, possibly more difficult question, if you go to http://kurtiskronk.com/weddings I am using SlideShowPro (flash) w/ SSP Director (self-hosted) as the back-end for it. When it pulls up a new image, it adds the following after /weddings ... "#id=album-152&num=content-9698"
There are four sections of the portfolio
# Homepage (kurtiskronk.com) id=album-148 ($id is constant for this section)
# Weddings (/weddings) id=album-152 ($id is constant for this section)
# Portraits (/portraits) id=album-151 ($id is constant for this section)
# Commercial (/commercial) id=album-150 ($id is constant for this section)
Assuming we get kurtiskronk.com/weddings to rewrite successfully without breaking anything, how would we make the total URL something cleaner kurtiskronk.com/weddings/9698 since the $num is the only thing that will change within a given section?
Kurtis, thanks for the extra information. It's a lot easier to give a specific answer to this.
My first comment is that you need to separate out in your thinking URI space -- that is what URIs you want your users to type into their browser -- and filesystem space -- what physical files you want to map to. Some of your mappings are URI->URI and some are URI->FS
For example you want to issue a permanent redirect of www.kurtiskronk.com/* to kurtiskronk.com/*. Assuming that you only server the base and www subdomains from this tree, then this cond/rule pair should come first, so that you can assume that all other rules only refer to kurtiskronk.com.
Next, you need to review the RewiteBase documentation. .htaccess files are processed in what Apache calls a Per-Directory context and this directive tells the rewrite engine what to assume as the URI base which got to this directory and .htaccess file. From what I gather, your blog is installed in docroot/blog (in the filesystem, and that you want to get to directory by typing in http://kurtiskronk.com/blog/ but that this .htaccess file is for the root folder -- that is the base should be (this goes before the www mapping rule)
DirectorySlash On
DirectoryIndex index.php
RewriteBase /
#301 redirect to domain without 'www.'
RewriteCond %{HTTP_HOST} ^www\.kurtiskronk\.com$ [NC]
RewriteRule ^(.*)$ http://kurtiskronk.com/$1 [R=301,NC]
You can add some field dumps look for REDIRECT_* in the Server or Environment table in the phpinfo O/P to see if these are sensible. For example:
RewriteWrite ^(.*)$ - \
[E=TESTDR:%{DOCUMENT_ROOT}/pages/$1.php,E=TESTPDR:%{DOCUMENT_ROOT}/pages/$1.php]
Your next rule is that if the file exists in the subdirectory pages then use it:
RewriteCond %{DOCUMENT_ROOT}/pages/$1 -f
RewriteRule ^(.*)$ pages/$1 [NS,L]
[Note that some shared service sites don't set up DOCUMENT_ROOT properly for the rewrite engine so you may need to run a variableinfo script (<?php phpinfo(INFO_ENVIRONMENT | INFO_VARIABLES); to see if it sets up alternatives. On your site you have to use %{ENV:PHP_DOCUMENT_ROOT} instead.]
Your next rule is that if the file exists, but with the extension .php in the subdirectory pages then use it:
RewriteCond %{DOCUMENT_ROOT}/pages/$1.php -f
RewriteRule ^(.*)$ pages/$1.php [NS,L]
Now redirect any blog references to the blog subdirectory unless the URI maps to a real file (e.g. the blog stylesheets and your uploads.)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^blog/ blog/index.php [L]
A complication here is that WP may be using a poorly documented Apache feature call Path Info that is a script can act as a pseudo directory so http://kurtiskronk.com/blog/tag/downtown/ is redirected to docroot/blog/index.php/tag/downtown/ which is then executed by `docroot/blog/index.php using /tag/downtown/ as the PATH_INFO. But this is one for Wordpress experts to comment on. If this last rule doesn't work then try:
RewriteRule ^blog/(.*) blog/index.php/$1 [L]
PS. I like your site. I wish I was that young again :(
Postscript
When you say "it doesn't work", what doesn't with this .htaccess?
http://kurtiskronk.com/phpinfo,
http://kurtiskronk.com/phpinfo.php,
http://kurtiskronk.comblog/tag/downtown/
It's just that these rules work for these tests (with domain swapped) on mine. (One way is to move or copy the above variableinfo.php to the various subdirectories. If necessary temporarily rename the index.php to index.php.keep, say, and copy the variableinfo.php to the index.php file. You can now enter the various URI test patterns and see what is happening. Look for the REDIRECT_* fields in the phpinfo output, and the SCRIPT_NAME will tell you which is being executed. You can add more {E=...] flags to examine the various pattern results. (Remember that these only get assigned if the rule is a match.
Lastly note the changes above especially the additional NS flags. For some reason mod_rewrite was going directly into a subquery which was resulting in redirect: being dumped into the file pattern. I've had a look at the Apache code and this is a internal botch to flag that further redirection needs to take place (which then replaces this or backs out). However this open bug indicates that this backout can be missed in sub-queries and maybe that's what is happening here. Certainly adding the NS flas cured the problem on my test environment.
PS. Note the added explicit DirectoryIndex directive and also that whilst http://kurtiskronk.com will run the root index.php, the explicit /index.php version will run the one in pages, because that's what your rules say.
Here is a simple solution. You can use it apache conf file(s) or in .htaccess (easier to set up when you're trying).
mod_rewrite has to be enabled.
For example, use .htaccess in your DocumentRoot with:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /pages/$1.php [L]
It will redirect /about to /pages/about.php, and any other page.
The "RewriteCond" part is to authorize access to an existing file (eg: if you had an "about" file at the root of your site, then it will be served, instead of redirecting to /pages/about.php).
Options +FollowSymLinks
RewriteEngine On
RewriteRule /([0-9]+)$ /pages/$1.php [L]
Put something like this in your .htaccess file. I guess that is what you want.
Juest a redirect from a simple url to a longer url.

Categories