Clean URL assistance php/.htaccess - php

I'm looking to make some cleaner urls so that it basically, the web address looks nicer.
currently my url looks like this ..
...co.uk/article.php?sport=football&id=23.. & title for the post could be.. erm .."this is the title"
i have been looking around the web and all over youtube trying to find a video to help as i hardly take any info in when i read stuff, so videos seems more productive for me. What i would like is my url to read something like...
...co.uk/sport/football/this-is-the-title .. and if it was another topic something like...
...co.uk/sport/tennis/this-is-another-title ...something nice and clean and tells people exactly what they're looking at. Also, i would like to have it so when its on pages, them pages dont have to have there extension added on so you dont have to put .php or .html on the end of the address as well...

You must keep id also in the pretty URL like the URL structure of Stackoverflow here. So instead of: /sport/football/this-is-the-title have it like:
/sport/football/23/this-is-the-title
Once that is settled let's now look into establishing mod_rewrite code to achieve it.
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^sport/([^/]+)/([^/]+)/([^/]+)/?$ /article.php?sport=$1&id=$2&title=$3 [L,QSA]

Related

CMS Pages in Database .htaccess configuration

In my CMS I store all pages in a database. If you request a page currently it works like this:
Requested URI: www.xyz.com/site.html
.htaccess: mod_rewrite creates: /index.php?q=site
index.php: Looks up in Database for the entry site.
To clean up the URLs I like to have URLS like this: www.xyt.com/about/site.html or www.xyt.com/about/groups/groups.html
In my Database is an entry for every Page called owner which represents the Parent Site.
The Problem for me is that the number of 'folders' is not fixed.
So i thought I should Change www.xyt.com/about/site.php to /index.php?q=about-site in the .htaccess and than write a PHP function which finds the site site with the Parent about
What would be the RewriteRule?
I that a good way or is there an other (better) way?
Changing the foo/bar/about/site/etc.html to index.php?q=foo-bar-about-site-etc is much more difficult with mod_rewrite than it is with PHP. Just do this in php, get the $_GET['q'] variable and explode the string into an array or something using the / flags. It's also better this way because you'll know for sure that the / characters are reserved and you won't end up having to resolve stuff like /foo-bar/about/site. The rules would look something like:
RewriteEngine On
RewriteCond %{THE_REQUEST} \ /+index\.php\?q=([^&\ ]+)
RewriteRule ^ /%1.html [L,R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)\.html$ /index.php?q=$1 [L]

How to create a directory with a single file in it?

I'll be signing businesses up to advertise on my website, and I want them to have a direct URL for their customers to go to.
Like, instead of www.website.com/page.php?id=324234234,
I want to have www.website.com/businessname
Is there a simple way to do this? I've searched and seen a whole bunch of different things people are trying to do but I haven't seen anything that's the same as what I want to do.
I'm using a VPS, and I want to make sure that I don't open up permissions so that anyone can get in there and mess things up.
Also, these users will not be signing themselves up. I will be doing that.
The simplest way to get my end result is what I'm looking for. Thanks!
Basic URL rewriting could work.
Add to your .htaccess file
RewriteEngine on
RewriteRule ^(.*)$ page.php?businessname=$1 [L]
Then use PHP to rewrite the businessname to the ID of the company / find the data.
Of course .htaccess rewrite rules is a complete science if you need more complex rewriting...
Re-iterating what jtheman said with a little more explanation:
Create a file named .htaccess with the contents:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ page.php?businessname=$1 [L]
You need, of course, the ability to have directory level .htaccess enabled - you're using a VPS so you should be able to do this if it is not already enabled.
So let me explain what each line will do.
RewriteEngine on
Turns on the ability to URL re-write
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
Tells Apache not to re-direct files that exist in the directory already
RewriteRule ^(.*)$ page.php?businessname=$1 [L]
This is where the magic happens.
^(.*)$ this part is like a regular expression match. It will tell Apache to collect any URLs that have any characters within them and redirect them to page.php?businessname=(.*)
So, if you post:
www.website.com/stackover
It will really be sending: www.website.com/page.php?businessname=stackover
Then you can just use $_GET[businessname] to dynamically update the page.
Hope this helps!

php url shortening

Ok i guess this is a bit of a general question really as opposed to a problem.
I am building a system where users can share their profile on other websites so i am wondering is it possible to shorten the actual url which would provide a link to their profile which would be something like this, www.somedomain.com/users/profile.php?user=myusername to simply cut out the users folder and the profile page and so something like this: www.somedomain.com/myusername
I have seen lots of url shortening scripts but they don't seem to do this, any suggestions or advice would be appreciated.
Thanks
What you're looking for is called URL rewriting and can be done using Apache's mod_rewrite. You would place a file called .htaccess in your root web directory and it would contain a snippet like this:
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule /(.*) /users/profile.php?user=$1
this is called "url rewriting" - there are different approaches to do this, for example using apaches mod_rewrite. another way would be to manually parse $_SERVER['REQUEST_URI'] - this would make your site work even if mod_rewrite isnt enabled, but is some more work.

Understanding .htaccess

I am trying to change my url type with .htaccess but I have a few problems. I tried a few online tools but they even do not work for me. So here what I am trying to do;
I have pages like http://mydomain.com/profile.php?u=newuser and I want to make as this: http://mydomain.com/newuser but so far I could not achieve it.
Here also what I have tried;
Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*) profile.php?u=$1
After making changes in .htaccess, do I also have to make any changes in my php files? Also, when try to open http://mydomain.com/newuser I noticed that some of my images on the page are disappearing, what would be the reason for that? Thank you so much guys!
You've gotten quite far, but now you're rewriting every possible url in your domain to profile.php, including stuff like /images/logo.jpg for example.
The question is, what do you want to do with this? An easy way to go about is changing it to this:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) profile.php?u=$1
The added 'RewriteCond' causes the rewriteengine to only rewrite urls that don't exist on the server, so your images will show up fine.
Personally, I think it might be better to add a /profile/ prefix to all your profile urls:
Options +FollowSymLinks
RewriteEngine on
RewriteRule profile/(.*) profile.php?u=$1
This will allow you to add new rewrite rules in the future, if you need them; it will also not give you issues if one of your users decides to go for a username called 'profile.php' or anything else that clashes with the existing urls on the server.
This is because you're sending every request to profile.php (.*). This is going to affect all the requests for images, resources, etc.
Add this line above your rule to exclude "real" resources:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

how to send data in url in a different way then by using get method

I want to my adress look like this: www.example.com/112112/example
Where 112112 is a data, that I want to work with in a php script. How to do this? For some reasons, I dont want to the adress look like www.example.com?id=112112
You can use url rewrite to achieve the binding from www.example.com/112112/example to www.example.com?id=112112
For Apache webserver, you will find here a guide: http://httpd.apache.org/docs/2.0/misc/rewriteguide.html
This is about mod_rewrite and .htaccess-files, add a .htaccess-file to your webserver root and add this piece of code:
RewriteEngine On
RewriteRule ^(.*)/ index.php?id=$1
The easiest way to do this is if you're on a system using the Apache web server, along with mod_rewrite. To get the desired effect, you'd put a .htaccess in your directory with something like this:
RewriteEngine on
# Dont rewrite files or directories which actually exist
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite for example.com/100/example to example.com/page.php?id=100
RewriteRule ^([^/]+) page.php?id=$1
You can have lots of rules to cover different possibilities.

Categories