简体   繁体   中英

Clean/Simple URL using .htaccess

I have a HTML form which looks like this:

<form action="process.php" method="get">
    <input type="text" id="q" name="q" maxlength="16">
</form>

The process.php spits out some data, that's all working fine. When it has returned the data the URL looks like this:

website.com/process.php?q=banana

I would like to have a cleaner URL like this:

website.com/banana/

Is this possible using .htaccess? Thanks in advance!

Put this code in your DOCUMENT_ROOT/.htaccess file:

RewriteEngine On

# external redirect from actual URL to pretty one
#RewriteCond %{THE_REQUEST} \s/+process\.php\?q=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=302,L]

# internal forward from pretty URL to actual one
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
#RewriteRule ^([^/]+)/?$ /process.php?q=$1 [L,QSA]

I think you're going to have to use method="post" to hide the Query String in the URL. I can't think of any way to dictate the format of the URI when using a form submission, but maybe someone else can come up with something.

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