Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

htaccess rewrite rule with single parameter

When user enters url like

http://example.com/app/abcd123/

I want to show hime page from

http://example.com/app/index.php?param=abcd123

Without changing URL in browser.

I put .htaccess file inside app folder with code

RewriteEngine on
RewriteCond %{REQUEST_URI} ^/index.php [NC]
RewriteCond %{QUERY_STRING} ^param=(.*)
RewriteRule (.*) http://example.com/app/%1? [R=301,L]
like image 813
moonvader Avatar asked Aug 17 '26 18:08

moonvader


2 Answers

You can use this code in /app/.htaccess:

RewriteEngine on
RewriteBase /app/

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

# internal forward from pretty URL to actual one
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+?)/?$ index.php?param=$1 [L,QSA]
like image 190
anubhava Avatar answered Aug 19 '26 09:08

anubhava


Try this .htaccess code. It should help you.

Make sure the rewrite base should be the form the root to your present directory.

RewriteBase /app/

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-s
RewriteRule ^(.*)$ index.php?param=$1 [QSA,NC,L]

RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*)$ index.php [QSA,NC,L]

RewriteCond %{REQUEST_FILENAME} -s
RewriteRule ^(.*)$ index.php [QSA,NC,L] 

In this, if the user enters like http://example.com/app/qwerty the call will be processed like http://example.com/app/index.php?params=qwerty

This should be working, I tested it. Let me know if you face any troubles.

like image 21
Sibidharan Avatar answered Aug 19 '26 07:08

Sibidharan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!