I have a URL that looks like this: https://www.example.com/users.php?username=Name. Instead of this I want it to look like this: https://www.example.com/users/username=Name. What are the steps to aquire this? I have tried searching, but I just cannot figure out what to search to find the answer.
It depends if you're using an apache server you can acquire this with .htaccess, in that case you could use RewriteEngine On with the specific RewriteRule.
.htaccess file in the root of you're website/domain:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^users/username=(.*)$ /users.php?username=$1
Beside this there are a lot of more options and flags with htaccess rewriterule
If you're using nginx it will be done on the nginx way, you may use an converter from htaccess to nginx of fins some more information for nginx rewrite url.
Related
I store data in text file.
And when user enter in address bar something like
my_syte.com/aaa - (without extension)- I need to file_get_contents aaa.txt file
my_syte.com/bbb - I need to file_get_contents bbb.txt file
Please advise the most powerful way of do it. Apache server.
Thanks
On Apache servers you can use mod-rewrite in .htaccess file:
RewriteEngine on
RewriteRule ^([a-zA-Z]+)$ /$1.txt [L]
if your files can contain - or _ or numbers then use:
RewriteEngine on
RewriteRule ^([a-zA-Z0-9_-]+)$ /$1.txt [L]
On nginx servers it's more complicated but some of them works with .htaccess. On other servers there may be entirely different approach. It's hard to help you without more informations.
As you said it's Apache, then use examples above. Either edit or create .htaccess file on your webroot (directory which is accessed by domain). First check if it were there (could be hidden) and if it exists then only edit it (add lines at the top).
If it doesn't exist, then create one by yourself.
Can you please give us some insights about your server? Apache nginx?
In Apache, you can achieve that with url rewriting.
Enable mod_rewrite in apache
Put the following line of code in .htaccess on the same location of my_site.com/
RewriteEngine on
RewriteRule ^/foo$ /foo.txt [PT]
to make it generic
RewriteEngine on
RewriteRule ^/*$ /foo.txt [PT]
Maybe I am wrong in sytax based on your specific server configuration. You need to make the best possible regular expression for this case.
After reading tons of SO questions, asking friends and so one, I'm coming here with a strange issue regarding Apache mod_rewrite.
I'm trying to catch http://api.server.com/.../results.php?id=X URL though a RewriteRule.
Quite simple you'll say, I know it, my .htaccess file content is :
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^results/(.*)$ results.php?id=$1
results.php is quite simple for debugging reasons, looks like
var_dump($_GET);
But this script always return array 0 { }
Shall I specify that I've already tried to clear the flags, and change the (.*) class by others, without effects.
Thanks for your help.
You will need to disable MultiViews option here:
Options +FollowSymlinks -MultiViews
RewriteEngine on
RewriteRule ^results/(.*)$ results.php?id=$1 [L,QSA,NC]
Option MultiViews is used by Apache's content negotiation module that runs before mod_rewrite and makes Apache server match extensions of files. So /file can be in URL but it will serve /file.php.
Your rewrite rule does not match the URL you are using (http://api.server.com/customer/2/results.php).
The correct URL according to your rule and setup is:
http://api.server.com/customer/2/results/123
However, you mention having placed everything in the /2/ folder. If 2 is the ID you are trying to get, it cannot work -- URL rewriting only works with non-existing paths.
Instead you should place your .htacess and results.php file in the customer folder, and use the following URL:
http://api.server.com/customer/results/2
Or change your rule and URL to:
RewriteRule ^([0-9]+)/results$ results.php?id=$1
http://api.server.com/customer/2/results
I have a site/blog that pages URL as www.example.com/sample-page and I want to make it like www.example.com/p/sample-page.html For this purpose I am thinking to use .htaccess file to write rewrite/redirect rule to do it. So can we make it using .htaccess?
You probably should be looking at the mod_rewrite documentation for apache (the rules in there can be placed into a .htaccess file).
http://httpd.apache.org/docs/current/rewrite/remapping.html
However, for your specific case (which I think is to end up mapping the real URL www.example.com/WP/p/sample-page.html to www.example.com/WP/sample-page), you could do something like this:
RewriteEngine on
RewriteRule ^WP/sample-page$ WP/p/sample-page.html [NC]
In my website there is a link to registered tutors. That link is something like this
http://www.lankainstitute.com/profiles/centers/index.php?code=1203&institute=Montana+Higher+Educational+Institute#page=art-section
So I need to rewrite this link and after rewrite it want to display in my browser's address bar something like this:
www.lankainstitute.com/1203/Montana-Higher-Educational-Institute
Can anybody tell me is it possible to rewrite above original URL to my expected URL and can display it in address bar?
Thank you..
Yes, you can do this by adding a line in your .htaccess file.
Depending on the server (Apache, ISS) there are tool to do so. Like in Apache mod_rewrite, look for some sample rewrite rules.
for example
DocumentRoot /var/www/example.com
Alias /myapp /opt/myapp-1.2.3
<Directory /opt/myapp-1.2.3>
RewriteEngine On
RewriteBase /myapp/
RewriteRule ^index\.html$ welcome.html
</Directory>
learn more to use htaccess for url rewriting
Well, in this case, you can simply do this:
http://www.lankainstitute.com/profiles/centers/index.php?code=1203&institute=Montana+Higher+Educational+Institute#page=art-section to www.lankainstitute.com/1203/Montana-Higher-Educational-Institute
Code:
RewriteEngine On
RewriteRule ^([0-9]+)/([a-zA-Z0-9_-]+)$ index.php?code=$1&institute=$2
Add this code in your .htaccess file
Use your .htaccess file.
Look into RewriteEngine
I'm in a situation where my web page have the following types of URL's
http://mysite.com/?type=home
http://mysite.com/?type=aboutus
http://mysite.com/?type=contactus
For the end users I need it to display like the following:
http://mysite.com/home
http://mysite.com/aboutus
http://mysite.com/contactus
This means, the user is not aware that the URL have a request parameter "type" in it.
I'm not sure it is possible or not.
Please help me to get to a solution.
EDIT: I searched lots of websites to learn URL rewrite by using .htaccess, But didnt able to make them working in my server.
Place this in you .htaccess file
RewriteEngine On
RewriteRule ^([a-zA-Z0-9-+/]+)$ ?type=$1
RewriteRule ^([a-zA-Z0-9-+/]+)/$ ?type=$1
Yes it is absolutly possible.
Your have to be sure your hostprovider has enable URL rewriting (I don't really remenber but I'm sur you can find help on Internet to verify that).
You have to create an ".htaccess" file at the root of your site.
Put this content in it :
tell the server to follow symbolic links :
Options +FollowSymlinks
activate the rewrite module :
RewriteEngine on
Rule for rewrite url
RewriteRule ^([a-z]+)$ /index.php?type=$1 [L]
Note this is just a qucik exemple you may want to look "url rewriting tutorial" on Google.