In my php web site, I need to display my urn in a specific manner
I need to hide the query string parameters and just want to display the value in the URL
i.e.
mydomain.com/city.php?place='usa'&id=2
I need to display
my domain.com/city-2/usa
Is it possible?
Please help me. I am in a serious situation.
Thanks in advance
Yes, it is possible:
RewriteEngine On
RewriteBase /
RewriteRule ^city-(\d+)/(.*) city.php?place=$2&id=$1
You need to put it into the .htaccess placed in the web root of your site. To get it worked you need to have .htaccess parsing enabled and mod_rewrite apache module (assuming you have apache as a web server) enabled as well.
More in official documentation
Related
Hi I need to rewrite URL in Wordpress.
I have a website where pages are created thanks to data loaded from JSON files.
These data allow me to create custom links that send the parameters via the URL.
I set the permalinks with /% postname% /
Example:
https://example.com/postname/?lid=16036&name=John&st=2019-2020
I need to transform in
https://example.com/16036/John/2019-2020.html
To do that the rule is
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)/([^/]*)\.html$ /postname/?lid=$1&name=$2&st=$3 [L]
Added in htaccess but nothing changes
I use a plugin "Admin Tools for Wordpress" that allow me to change htaccess
The support says:
I don't think that you are on the right path. It sounds like you're trying to convert
https://example.com/postname/?lid=16036&name=John&st=2019-2020
to
https://example.com/16036/John/2019-2020.html
in the HTML output of the page.
Apache Rewrite cannot do that. And before you ask, no, Admin Tools has nothing to help you with that nor can it possibly have such a feature due to the way WordPress pages are generated (there is no document buffer like there is in Joomla)
Can anyone tell me if there is any way to do this?
I am trying to find out how websites like Imgur, MEGA and such are able to do this:
https://imgur.com/a/SbmNz (emphasis on SbmNz)
The SbmNz bit is dynamic between images or files and I guess it is a kind of $_GET. And I was just wondering how you can do this without the usual ?name=value way.
you can use any MVC or just write a htaccess rule for it , for instance, you can use laravel to pass variables along with url
for laravel refer URL Generation-Laravel5
to write .htaccess refer USING .HTACCESS REWRITE RULES
You can do with Apache rewriting module by changing .htaccess (Create .htaccess file in your folder)
For example if you have this snippet
RewriteRule ^play/([^/]*)$ player.php?id=$1 [L]
When user visits www.example.com/play/Trg4 in backend request is actually handled for www.example.com/player.php?id=Trg4
Then you can get the id with $_GET['id']. it means there is no change with php code. it is completely done with .htaccess
Make sure you enabled rewrite_module of your Apache server
I am using PHP in a Google cloud compute engine container. The following .htaccess code should retrieve a query string param g - but somehow there is no query string parameter that is being retrieved.
Options +FollowSymlinks
Options All -Indexes
RewriteEngine On
RewriteRule ^([^/]+)(.sh)/?$ index.php?g=$1 [QSA,L]
By way of example:
http://example.com/dir/dallas.sh should be able to retrieve dallas as the value of query string parameter g? But it is not doing that?
The .htaccess file with the rewrite code is in the subdirectory dir with index.php that is looking for the query string parameter g.
I have tried to test if .htaccess itself is working. I can prevent directory browsing by putting in the code in the .htaccess file for the directory:
Options -Indexes
Thanks #w3dk - this is now working. Mod_rewrite module loads in httpd.conf. I restarted the apache server. I am able to now retrieve Dallas from the querystring. No idea if I was doing something wrong. No idea if there was something reset by Google Support. Is any one familiar with similar intermittent on-off on querystring retrieval from htaccess in apache on google cloud. They officially DO NOT support htaccess in their app-engine containers or in firebase. Curious. Any way? false alarm. Thanks a lot #w3dk for your responses.
I have a website that passes some GET variables to different pages in PHP. My issue is that now I have a url with variables i.e. index.php?category=categoryname and that's not very memorable.
Is there any way I can change the URL to something like /categoryname instead without duplicating the page and storing in folders? But also allow users to type in /categoryname and be redirected to the correct page?
.htaccess Apache mod_rewrite, almost every professional dynamic website uses this method (like stackoverflow).
The method is fully explained in this article far better then I could ever explain it in this answer box.
You should look into writing some apache Mod_Rewrite rules in a .htaccess file.
The solution is discussed here:
this is done by the rewrite module of apache and this handles regular
expressions. You have to put a rule
like this in your .htaccess file on
the root of your website:
RewriteRule ^cat/([0-9]+)$
/index.php?category=$1
^ means the start of the url after
www.example.com/ $ means the end of
the page.
www.example.com/cat/123
will be converted by the server to:
www.example.com/index.php?category=123
In PHP you use the normal $_GET['id']
variable. The rewrite module must be
enabled by apache... This is mostly
used to make the url format
independent of the serverside
scripting language so the .php in the
url is not logical. Thats why i
changed it to product/ . The .htaccess
starts with
RewriteEngine On Options
+FollowSymLinks RewriteBase / Here all the rewrite rules.. ...
Hey guys I have a question. I wanna create a profile page for each new user, and I noticed that on facebook you could simply type in www.facebook.com/username and you get to the user's page, my question is, how can I do this without something like domain.com/users.php?useraname="username" or something like that? How can I simply make it like the facebook one?
What you are looking for is mod_rewrite. This will allow you to write PHP code that appears to the end user to be a directory on the server (such as www.facebook.com/user.php?username into www.facebook.com/username.)
An introduction to them with PHP can be found here: http://wettone.com/code/clean-urls
Please note you will need to enable it on your server. That should be possible in the .htaccess file if you're running an Apache server.
This is not a complete answer since I'm NOT a php guy
What you're looking for a RESTful urls, mostly you can get urls like that on your web app if you use a framework that supports restful urls
See this SO question:
REST-style URLS and PHP
See this article:
http://blog.garethj.com/2009/02/building-a-restful-web-application-with-php/
Search google and Search SO with google
This can’t be done with PHP alone. It’s the web server that needs to know how to handle these kind of request first.
Because, to put it simply, a web server just takes the requested and tries to map it onto a file in the file system below the document root directory. And if it can’t find an appropriate file, it returns an 404 error code.
Now there is some kind of URL rewriting mechanism for almost every web server software. In case of Apache as the most popular web server software out there, there is mod_rewrite that allows URL rewriting based on rules. In this case the following could enable /users.php?username=username being also accessible through /username:
RewriteEngin on
RewriteRule ^[a-z]+$ index.php?username=$0
Options +FollowSymLinks
RewriteEngine On
RewriteBase /php/profile
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^users/(.)$ ./profile.php
http://exapmle.com/users/waqar.alamgir
in $_SERVER['REQUEST_URI'] you will see users/waqar.alamgir