How to fake a url? - .htaccess

I have a url as http://mydomain.com/levels/home?mode=48bb6e862e54f2a795ffc4e541caed4d.
I need to make fake url of above url. When I type a url http://mydomain.com/medium in address bar of browser, request needs to go in url http://mydomain.com/levels/home?mode=48bb6e862e54f2a795ffc4e541caed4d, but address bar will need to show this url http://mydomain.com/medium itself.
How can we do this with .htaccess file?
EDIT:
I tried with
RewriteEngine On
RewriteRule ^medium.*$ /levels/home?mode=48bb6e862e54f2a795ffc4e541caed4d [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]
But this will redirect to original url /levels/home?mode=48bb6e862e54f2a795ffc4e541caed4d and showing this url instead of /medium url in address bar.

Use this
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule medium.php(.*)$ levels/home?mode=48bb6e862e54f2a795ffc4e541caed4d?$1 [L,QSA]

Related

htaccess RewriteRule shows 404 error and does not hide index.php

I have created a url shortener. This is my .htaccess file:
RewriteEngine on
RewriteRule ^([a-zA-Z0-9]{6})$ index.php?redirect=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) /index.php/$1 [L]
When I type example.com/Ran41D I get 404 error, when I type example.com/index.php I can see the filename in address bar.
I expect example.com/Ran41D to forward the url to example.com/index.php?redirect=Ran41D - which is working great, and to see in address bar example.com/ instead of example.com/index.php

htaccess rewrite going to 404?

for the last hours I cant seem to figure out why my .htaccess file is redirecting to a blank url. Here is my htaccess file below.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-l
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteCond %{QUERY_STRING} ^id=([0-9]*)$
RewriteRule ^(.*)$ /test/user-profile/%1? [R=301,L]
I am trying to rewrite www.example/test/user-profile?id=4 ->>> www.example/test/user-profile/4
It does rewrite the url, however then the page has "The requested URL was not found on this server." the htaccess file is in public_html folder, so I am trying to select a specific user inside www.exampleurl.com/test/userslist.php which would go to www.exampleurl.com/test/user-profile.php. It all works perfectly before using htaccess. www.example/test/user-profile?id=4 ->>> www.example/test/user-profile/4 but it just wont find the url or file? im a super noob folder path below -> public_html/test/userslist.php & user-profile.php
Just cant seem to figure out what to do. Yes I do have mod-rewrite on.
With your shown samples, please try following htaccess rules file.
This assumes that you are hitting link www.example/test/user-profile?id=4 in browser which is redirecting to www.example/test/user-profile/4 and is being served by index.php with parameters user-profile=user-profile in your url and id=digits in url. You can also change them as per your need.
Make sure to clear your browser cache before testing your URLs.
RewriteEngine On
RewriteBase /test/
RewriteCond %{THE_REQUEST} \s/(test/user-profile)\?id=(\d+)\s [NC]
RewriteRule ^ %1/%2? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^([^/]*/)/([*/]*)/?$ test/user-profile.php?user-profile=$1&id=$2 [QSA,L]
2nd solution: In case someone is hitting url example.com/test/user-profile/7 then try following htaccess rules.
RewriteEngine On
RewriteBase /test/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^([^/]*/)/([^/]*)/(\d+)/?$ test/user-profile.php?user-profile=$1&id=$2 [QSA,L]

URL rewriting without redirect

I have a couple of directives which should redirect the user to the right path:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)$ user?user=$1 [QSA]
So that writing www.mywebsite.com/myprofile it displays www.mywebsite.com/user?user=myprofile
It works properly, but it redirects to that URL.
What I would like is that my browser displayed www.mywebsite.com/user?user=myprofile (which is an existing folder on my server), but still showed www.mywebsite.com/myprofile on the address bar, which is the way many websites (as far as I know) create your own profile page.
How is it possible?
you could try just as this:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)$ user/?user=$1 [QSA]
or
RewriteRule ^([^/]+)$ user/index.php?user=$1 [QSA]
With the slash added, the URL is not redirected by only rewritten.

I can't rewrite dynamic url to static

I want to change dynamic urls of my site to static urls for better SEO. I have tried a lot of combinations but any of them didn't work.
My url is this index.php?page=something and i want to convert it to page/something
I have tried combinations like following
RewriteEngine On
RewriteRule ^page/([0-9a-z_-]+)$ index.php?page=$1 [NC,L]
Or
RewriteEngine On
RewriteRule ^page/(.*)$ index.php?page=$1 [NC,L]
When I type in my browser mydomain/page/something returns to me my page with navbar and footer but without any content.
Update
That was the problem: PHP problems with current url
RewriteEngine On
#if the requested url isn't a file or a dir or an image
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.gif|\.jpg|\.png)$ [NC]
#process index.php
RewriteRule ^([^/]+)/page/?$ /index.php?page=$1 [L,QSA,NC]

htaccess rewrite rule to hide actual file

This is my folder structure
htdocs
-> testing
->index.php
->.htaccess
Inside index.php I have this code
<?php
if(isset($_SERVER['PATH_INFO'])) {
echo $_SERVER['PATH_INFO'];
}
?>
Inside .htaccess I have this code
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php/$1 [NC,L]
Here is my question:
In the above htaccess rewrite rule, which will link my address localhost/testing/HelloWorld to localhost/testing/index.php/HelloWorld
Is it possible to do some thing like this? When the user enter localhost/testing/index.php/HelloWorld I want the browser to hide the word index.php and only display something like this localhost/testing/HelloWorld
I tried to redirect the link localhost/testing/index.php/HelloWorld to localhost/testing/HelloWorld by adding another rule in .htacces file but I get "The webpage has redirect loop" error message.
After I add in new rule the .htacces file looks like this
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php/$1 [NC,L]
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^index.php/(.+)$ /testing/$1 [R]
Does any ways that I could make my link like this? Thanks in advanced, your help will be very much appreciated.
You get the redirect loop, because the rewritten url matches the rule for redirection. You'll need to use a trick to make the external redirect only happen on an external request with index.php, not on an internal rewrite that maps a different request to that file. You can do this with %{THE_REQUEST} which will only ever be whatever the external request was. It will not update if you rewrite the file it maps to.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php/$1 [NC,L]
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /testing/index\.php/
RewriteRule ^index.php/(.+)$ /testing/$1 [R]

Resources