URL rewrite more than one php file - php

I got an problem with url rewriteing.
First of all i am starting to think that i am making mistake in the structure of my webpage.
I made more than one php file. I think that this is mistake because in all tutorials and samples urls they are using in .htaccess only index.php.
Info
1. I got in navigation tab index.php section1.php section2.php section3.php section4.php
2. In index.php i only have an basic information that won't change much.
3. In each section.php file i am echoing out list of articles.
4. each aricle got id and i am using it to echo out information when user clicks on article link.
5. link to article "http://www.example.com/section1.php?id=607575"
6. In each section.php file i got more than one page.
7. For now my links to section page looks like this "http://www.example.com/section1.php?page=1".
Questions
Qestions
Should i better echo out all information on index.php page by getting information from url?
If now my link looks like this <a href="http://www.example.com/section1.php?page=1>section1</a> do i need to manually rewrite them OR it dose not matter, .htaccess will do the job?
if i need to rewrite url will it looks like this? section1
How to rewrite urls so they look like this www.example.com/section1/page/1 and www.example.com/section1/article/607575 (i know it's bad to use only numbers, i just need to understand how it works,then i will figure it out,how to replace it with article name).

If I understand correctly, this is how you would rewrite:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ /path/to/section1.php?page=$ [QSA,L]
I'm not very used to mod_rewrite but that should work. You might have to rewrite rule for ?article too.
If this was not answered correctly, please refer to http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html

From: http://www.example.com/section1/page/1
To: http://www.example.com/section1.php?page=1
RewriteRule ^section(\d+)/page/(\d+)$ /path/to/section$1.php?page=$2
From: http://www.example.com/section1/article/607575
To: http://www.example.com/section1.php?id=607575
RewriteRule ^section(\d+)/article/(\d+)$ /path/to/section$1.php?id=$2

Related

How to create Clean URL for every page using .Htaccess?

I'm currently in the progress of creating a huge website, but instead of the regular URLs I'd like to use Clean / User Friendly URLs. I have been searching on how could I basically tailor these Apache Mod Rewrite rules for my needs, howere I could not found any solution for my particular problem.
Below you can read the aim, which I'd like to achieve with the URLs (I'm not going to write the domain name each time, just imagine: http://www.example.com ahead of the URL parts).
/register/ OR /register ---> /register.php (It should support both of the variations.)
I actually have more files for the registration and I'd like them to be accessible using the "part" words like:
/register/part1/ OR /register/part1 ---> /register.php?part=1 (It should support both of the variations.)
Also, what if I have more than just one query varialbe? (Like "personal=1")
/register/part1/personal/ OR /register/part1/personal ---> /register.php?part=1&personal=1
And what if I have many more of these queries, but I CAN'T specify all of them before? Any of these can be entered. (Like "thing,name,job,etc")
/register/part1/personal/Nicky/ OR /register/part1/personal/Nicky ---> /register.php?part=1&personal=1&name=Nicky
OR any kind of variations you can imagine:
/register/part1/personal/thing/employee/ OR /register/part1/personal/thing/employee ---> /register.php?part=1&personal=1&thing=1&job=employee
EDIT:
This is what I've tried yet, but it just redirects the pages to index.php :/
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
So I have given you a lot of examples, what I'd like to basically achieve. I can have a lot of other pages besides "register.php" so it shouldn't be specific to that page only. I also want that which is VERY important, that IF someone goes to for example: register.php?part=1 it should redirect them to the appropriate Clean URL (of course in PHP).
I would also want to ask what should I do in the PHP end to make everything good? I saw that Wordpress has a really great solution for this, which is pretty automatic, and it looks great!
Is there any ways that someone could please explain me how to create a great .HTACCESS mod_rewrite solution for this? I would be really-really glad!
Please do not mark this question as duplicate, because I really did not found anything specific for my case.
You mentioned WordPress, which has something like this:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
What this does is redirect any request that doesn't match a real file or directory to the index.php script. That script will then decide what page to display. It will do so by looking into $_SERVER['REQUEST_URI'] which holds a path like register/part1.
It would be easier for you to figure out what page to show using this method, because in PHP there are many ways to parse that path string, then map it to a function
You should be able to construct clean URLs like this from your htaccess:
RewriteEngine On
RewriteRule ^index\.html$ /index.php?pageID=Home [L]
RewriteRule ^about-my-homepage\.html$ /index.php?pageID=About [L]
RewriteRule ^contact-us\.html$ /index.php?pageID=Contact [L]
the first is the one you want to output (the "clean" URL), the second one the one you actually want to open. Good Luck!

How to create a simple but flexible Clean URL in .htaccess with Apache Mod_rewrite?

I have just started to be familiar with Apache Mod_rewrite and I have falied to achieve my goal.
I am building a website where I'd like to replace the ugly URLs with Clean, user firendly URLs. I have currently created some files already, but I'm not sure if I'll create clean urls, I'll be needing meny of these pages.
I'd have 2 questions, on how would professionals create this website, here we go:
1st question:
I have the following file structure which you see below:
Main folder:
index.php (Home Page)
login.php (Login Page)
register.php (Register Page)
Under Main Folder I have: Members folder:
index.php (Dashboard Page)
stats.php (Statistics Page)
The first problem/question is the following:
I'm currently just using the simple .php pages to access it's content. Would it be better to use 1 index.php file and include the necessary files when someone enters the following for example: /index.php?go=login
So would this be more professional than just providing the .php files and deliver their content when they're clicked?
2nd question:
This question would be around the Clean URLs. I would like to achieve the following:
When someone visits: http://www.example.com/index.php - it should redirects them to http://www.example.com/
When someone visits: http://www.example.com/login.php - it should redirects them to http://www.example.com/login/ - if someone types http://www.example.com/login that should also be processing login.php
As well as the other pages, like:
http://www.example.com/register.php <-> http://www.example.com/register/ (the slash should be optional)
http://www.example.com/members/dashboard.php <-> http://www.example.com/dashboard/ (the slash should be optional)
And so on...
I have already tried to create a rewrite condition, but it doesn't seems to work. Can anyone please update me on this?
What I've tried:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^login/?$ login.php [NC]
I really hope, that someone could help me to answer both of these questions. I have been searching for 2 days now, and I couldn't figure out what rewrite rule should I be using. Thank you really much!
EDIT:
Should I use a basic redirect and take care of the other codes using PHP? Please someone help me!
This (untested) code should take something.php and redirect to /something.
Additionally, it should allow you to keep doing stuff like /search?query=foo, which will be the equivalent of /search.php?query=foo.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^(test|login|whatever)/?$ $1.php/$2 [NC,L]

htaccess need help rewriting url

I've been trying to rewrite my URL's with a htaccess file.
My project is using my index.php as it's basic controller.
I fetch the pages from the database with the get-value "naam".
Example:
localhost/YourDesigns/YDCMS/index.php?naam=home (this shows the homepage)
localhost/YourDesigns/YDCMS/index.php?naam=about (this shows the about page)
Now i want to rewrite the URLS to be shown like this:
localhost/YourDesigns/YDCMS/home (this shows the homepage)
localhost/YourDesigns/YDCMS/about (this shows the about page)
I have done some htaccess stuff myself and i've successfully removed the "index.php" from the url, but the "naam" still remains.
How do i remove this?
This is my current htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /YourDesigns/YDCMS/index.php?naam=/$1 [L]
Thanks in advance :)
I think your way to see the process it's not totally right, if you want to show the url like you say localhost/YourDesigns/YDCMS/home you have to first replace the url inside your html content to that format, then by using a RewriteRule you call the correct path internally:
RewriteRule ^desired_url_path/([a-z0-9\-]+)$ /base_url_path/index.php?naam=$1 [L]
this way when an user click on a link the Apache server will use the above regex to convert the url by a rule that basically says : everything after the base url is aparameter value to be passed on the index.php.
Naturally the regex can be modified to suit your needs, the one i've written above it's a basic string pattern.

Redirect example.com/out/1234 to another URL

I know theres a lot of posts about redirects but this is a little different (I think).
Basically I want my outlinks to be example.com/out/1234 and I want them to go to a php that looks up the URL 1234 if referenced to in MySQL and the php header redirect to that URL.
The problem Im having is passing 1234 to a page. I know how if it was out.php?q=1234 but I want it to be /out/1234
Does there need to be an index file within an /out directory that also has a htaccess to rewrite it?
If so, any ideas what the regex need to be to do this? I have seen a few sites doing this and I cant work it out.
htaccess file in your document root, you can try adding:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?out/(.*)$ /out.php?q=$1 [L]
Replace the /out.php with whereever your php script for handling the URL is

URL Routing method for core php

I have web site in core php and I want to make my SEF URL.
In my web site for transferring control from one page to other i have used something like
header("Location: edit.php?id=12");
I read one article but couldn't get it how to implement it.
Link of article
So how i can make url to read like "mysite.com/usr/edit"
I read lot on google i got the idea that i need to do something in .htaccess but i dont know what needs to do with url in php so i dont have to change major code in site.
Thanks in advance.
You can do it with the .htaccess code given below:
RewriteEngine On
DirectoryIndex index.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
after creating .htaccess file with above code. You will have all the stuff after the file name ex. "/usr/edit" in the GET parameter $GET['url']. After that you can do anything.
Here is the working example of my MVC framework: https://bitbucket.org/ManthanB/my-framework/
You can contact me for further information.

Categories