3

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]
2
  • Is /app/ a real directory? Commented Apr 11, 2016 at 8:14
  • yes, and i put my htaccess file there Commented Apr 11, 2016 at 8:19

3 Answers 3

4

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]
Sign up to request clarification or add additional context in comments.

1 Comment

Wow both sides redirection(external to internal AND internal to external), this is great solution, thanks for sharing sir cheers.
1

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.

2 Comments

Thank you - code works and the only problem i faced - is that my fonts stoped to load because they are also in this folder and have links like "/app/fonts/font.ttf?ver=1.4.4"
@moonvader, anubhava's answer is the perfect one for your case.
0
RewriteEngine On
RewriteBase /app
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php?param=$1 [L]

Check it there: http://htaccess.mwl.be/ or other online htaccess test service

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.