简体   繁体   中英

short URL through htaccess file in 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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM