简体   繁体   中英

PHP: How to catch text written in url after my domain name

Please see the sample link/ senario below.

www.mydomain.com/khaled

Default page on "www.mydomain.com" is index.php. There is no folder or file named "khaled". When the above URL is typed, i want index.php to be called and check the name "khaled" in database and show the relevent profile.

Problem: When i type the above URL error 404 is displayed.

.htaccess is an option but dont know what to write in it. tried the following line but it returned "Internal server error".

RewriteRule ^([^/]+)$ index.php?i=$1&p=$2&%{QUERY_STRING} [L]

$_SERVER['REQUEST_URI'] will hold this information in PHP. Your htaccess redirect should look something like:

RewriteEngine On
RewriteRule ^([^/\.]+)/?$ /index.php?i=$1 [L]

Then in index.php $_GET['i'] will hold the value of whatever is in the URL after the domain

You can obviously add more parameters in the same way, such as:

RewriteEngine On
RewriteRule ^([^/\.]+)/([^/\.]+)/?$ /index.php?i=$1&j=$2 [L]

Then use $_GET['i'] and $_GET['j'] for your variables in PHP

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