Rewrite PHP all GET parameters .htaccess [duplicate] - php

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
mod_rewrite, php and the .htaccess file
Would like this to happen in the .htaccess file.
to rewrite
index.php?pagetype=name
to
/pagetype/name/
do this for all pagetypes so
?user=name
?post=number
?life=sucks ... etc
but would like to exclude
/css/
/images/
/js/
/php/
/template/
so the variable links still work.
I am guessing this should work but I want it in it's most simplest form so I don't have to change the .htaccess file everytime I add a pagetype.
I did a search for this but didn't find anything helpful.
The site will only have one physically accessed file: index.php then the rest are processed via $_GET commands

Just for the example you specified, the simplest approach would be:
RewriteCond %{REQUEST_URI} !^/(css|images|js|php|template)/
RewriteRule ^(\w+)/(\w+)/?$ index.php?$1=$2 [QSA,L]
(The RewriteCond could be written as negative lookeahead in the Rule instead of course.)
See also the mod-rewrite tag wiki for further howtos.

Related

using .htaccess file i want to edit url [duplicate]

This question already has an answer here:
Friendly Url .htaccess
(1 answer)
Closed 8 years ago.
my url is
http://altikalti.com/down.php?folderpath=53/143&imageid=29&file=17Mar2014-murzuq-band-ghat-festival-libya(altikalti.com).jpg&new
output url:
http://altikalti.com/53/143/29/17Mar2014-murzuq-band-ghat-festival-libya(altikalti.com).jpg
How can i do.. if any changes in panel setting then you can also suggest me
thank u for reading...
Have a good day
Create .htaccess file into root folder (e.g. http://altikalti.com/ location)
RewriteEngine On
RewriteRule ^(.*)/(.*)/(.*)/(.*) http://altikalti.com/down.php folderpath=$1/$2&imageid=$3&file=$4&new [L]
If you're not in production environment, you can check if mod-rewrite is enabled by
RewriteEngine On
RewriteRule ^(.*) http://www.google.com/ [L, R=301]
If it work every request should be redirect to google.com, which mean the first script should work and do the job for you.
This question has been asked several times already.
By searching with the keywords friendly url htaccess you can find this discussion that give an answer on the subject :
Friendly Url .htaccess
Or this one, especially for images :
Friendly URL for images
You can still follow this blog post for more complete examples :
http://surefirewebservices.com/tutorials/friendly-url-tutorial-pt1

How to make .php extension not appear on website? [duplicate]

This question already has an answer here:
Pretty URLs in PHP frameworks
(1 answer)
Closed 8 years ago.
I am using PHP for my website, and .php is appearing in my URLs. How can I remove this?
There are lots of ways you could achieve this, but they depend largely on your choice of web server.
If, for instance, you were using Apache HTTPD you could use:
MultiViews or
AddHandler (e.g. inside Files or FilesMatch) or
Alias or
mod_rewrite
… other options are probably available, but those are the ones that spring to mind.
Create a file called .htaccess ( the dot in .htaccess is not an accident ) and enter the following:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/(.*).php$
RewriteRule ^(.*)$ %1 [L,QSA]
Then save the file and place it a the root directory of your website.

URL structure /page.php vs. /page in PHP [duplicate]

This question already has answers here:
How can I remove file extension from a website address?
(11 answers)
Closed 9 years ago.
I have a build a few sites for my website. They all end with .php.
The problem is if you want to view the page you have to type in website.com/page.php instead of website.com/page. How do I make this happen for all of my main pages?
Is there a quick way of doing this, or do you have to set up a forwarding for all of the /pages to the /page.php?
In most cases this is achieved using MVC framework and Routing. It works in a way that you don't access single .php file for single web page you show to user. Every request goes through one file and you have a router where you define your routes and then define what action controller would that route invoke, and from there you choose what view file will you show to the user. Its hard to explain in few sentances. Anyway using MVC you get nice URL-s like www.example.com/controller/action/param
Now if you just want to remove .php extension from your files you can put this in your .htaccess file:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
But other then hiding .php, it wont do any good.
The best thing you can do is to read about MVC frameworks, Routing and Front Controller pattern, and take it from there, its not all about nice URL-s, there's much more to gain! And if you just want to hide .php extension then use above code.
Hope this helps!
You save your first file as index.php (index.php is the default page) and include or redirect all the other files internally. So there would be no reason to type a file name.
You can also use apache on .htaccess to rewrite your files, but you have to be careful with this.

Clean URL using PHP or Apache redirect [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Redirect *.php to clean URL
I am sure this has been asked here before, but for some reason whatever I am trying doesn't seem to work.
What I have is:
http://example.com/share/edit.php?id=59
What I want is:
http://example.com/share/59
My .htaccess is:
RewriteEngine on
RewriteRule ^share/([0-9]+)$ share/edit.php?id=$1
Is there something I am doing wrong? The .htaccess file is in example.com/share directory.
If the .htaccess is in the share directory then you don't need to specify the URL path. Try something like this:
RewriteEngine On
RewriteRule ^(.*)$ edit.php?id=$1 [QSA,L]
It also goes without saying that should you be using apache you will need mod_rewrite enabled.
Try to put your .htaccess to example.com directory.
Try giving it this way:
RewriteEngine On
RewriteRule ^share/([^/]*)$ /share/edit.php?id=$1 [L]
And your .htaccess file should reside in the root directory, i.e., example.com's directory,. where the share folder is there.
The '.htaccess' file is in 'example.com/share' directory.
Move it to the upper level directory (public_html or www).

Technique to create user friendly URLs [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to create friendly URL in php?
I have read a lot of content on Google about turning
profile.php?id=24 into something more user friendly.
How would I go about redirecting a link like
www.site.com/profile/username
to
profile.php?id=userid (find from username)
Thanks
This can be achieved with the Apache mod_rewrite RewriteEngine. An example .htaccess:
RewriteEngine On
RewriteRule ^/profile/username/([\d]+)$ profile.php?id=$1
You can do that with apache rewrite rules.
Apache will internally rewrite a URL like /profile/username to profile.php?username=username.
Example: (place this in a .htaccess in the same directory than profile.php)
RewriteEngine On
RewriteRule ^/profile/(.*)$ profile.php?username=$1
If you profile.php script doesn't accept a username parameter, you could also include the user id in the user friendly url:
RewriteEngine On
RewriteRule ^/profile/(.*)-(\d+)$ profile.php?id=$2
This will rewrite urls like /profile/username-id to profile.php?id=id
See apahe mod_rewrite documentation.
This is done by creating a RewriteRule in a .htaccess file, of by defining rules in httpd.conf.
This works on Apache. I'm, not sure how to do this on other servers.
When your users sign up, use a PHP or CGI script to make a file called "Username.txt" and store it in a folder called Profiles (as you have it). And inside the text file, make it generate the number (counting up or hashed?) Or use a rewrite service in Google or Apache.

Categories