URL rewrite in apache server - php

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.

Related

how to properly create url rewrite in php and htaccess

I know this question has been ask often. I leverage the links here but still cannot get it to work
source: link
what am trying to achieve:
I have Original URL:
Eg: http://localhost/followersid.php?id=101data
which am trying to rewrite as follows
Eg: http://localhost/followers
.htaccess Code
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)/\followers$ followersid.php?id=$1
My ISSUE: When I type http://localhost/followers in the url, it says The requested URL was not found on this server whereas followersid.php is in the same directory with .htaccess files
It was a regex issue. this is what solve the problem
RewriteRule ^([a-zA-Z0-9_-]+) followersid.php?id=$1

Site URL alias using mod_rewrite

I am developing a website, however I am trying not to have the unattractive url being displaced example instead of : /my_project/master_page.php?page=home and would like to display /my_project/project/welcome and so on, I am trying to achieve this by using mod_rewrite and .htaccess in the site's root directory.
The code is as follows below:
RewriteEngine On
RewriteRule ^project/([^/\.]+)/?$ master_page.php?page=$1 [L]
However I must add I do need some major assistance in achieving this, Thank You.

Go to file by link

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.

Strange issue using Mod_rewrite and $_GET variable

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

Automatic URL rewrite in .htaccess

At first I used IIS server for PHP, so I'm new to Apache (Windows platform). I am in some puzzle related with URL rewrite. As in my .htaccess file I use the below code for URL Rewrite.
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.*)\.aspx$ $1.php [nc]
Now If I browse a .php page with .aspx extension, it works well. eg If I browse
www.example.com/guwahati.php => www.example.com/guwahati.aspx (both URL works.)
But I want it automatically. Like if I type www.example.com/guwahati.php, it will automatically convert to www.example.com/guwahati.aspx, is it possible ? also URL rewritting is not working on my localhost. may anyone help me please.
But I want it automatically. Like if I type www.example.com/guwahati.php, it will automatically cconvert to www.example.com/guwahati.aspx, is it possible ?
It is possible, but by using redirect headers, not URL rewrites. Actually you could do an URL rewrite that redirects to a single file that replaces the target extension with aspx and then redirects to it. You may need to enable mod_rewrite in order for it to work.

Categories