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!
Related
Over the past few days, I have been working on a new website. As of now, I have chosen to go with something similar to that of an MVC: I am using PHP for routing to other pages depending on a value retrieved by GET, changing some settings inside the .htaccess.
When I search for my website on Google, I find three links: one for the website and two for different subdomains:
www.example.comsub.example.comone.example.com
The structure of the links on my website looks like this, because of the routing:
www.example.com/test/bedev.example.com/that/this
This is my .htaccess in case you need it:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
RewriteBase /
All the different parts of the website are linked together with a href, but I worry that may not be enough to make it fully SEO.
Is this a problem going to be a problem? Is there anything I can do about it?
Edit: what I am asking is whether or not this type of layout will be a problem regarding SEO. If that is the case, my follow-up question is what way I would go about fixing it, which most certainly would be a programming-related task (and so the question is not off-topic).
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
TL; DR: I would like to hit the index-api.php file if api is found in the URL, but then simply keep all other requests pointing to the site/dist directory as if it were the 'root' of the site.
So, I've spent way too many hours on this and trust me, I've dug through all of the resources for mod_rewrite. I guess I'm just not quite understanding and figured I'd ask on here.
What I want to do, in theory, seems simple. I'm building a single page application (Angular App) using Grunt, outputting that to a the root of a WordPress install. The WordPress install is simply serving up an API using the WordPress JSON API plugin, so I want the root of the site to hit my Grunt directory (located at site/dist/index.html), but all requests to siteurl.com/api to hit the index.php file and proceed normally.
Keep in mind I have other assets / images located in this site/dist directory, so ideally, it would be awesome if all requests to the site root would simply use this folder as the "base" of the site (e.g. a request to siteurl.com/images/testimage.jpg pulls from site/dist/images/testimage.jpg).
I feel like I'm onto something here and am surprised I couldn't find anything that directly tackles this issue.
What I've done now is renamed the index.php from WordPress to index-api.php and left it the same:
index-api.php:
<?php
define('WP_USE_THEMES', true);
/** Loads the WordPress Environment and Template */
require('./wordpress/wp-blog-header.php');
// phpInfo();
.htaccess:
<ifModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^api/(.*)$ index-api.php [L]
RewriteRule (.*)$ site/dist/index.html [L]
</ifModule>
I tried a myriad of other efforts from a few posts trying to get this working, and it seems to me like it should work fine. The funny thing is, if I comment out the last line RewriteRule (.*)$ site/dist/index.html [L] the api request works normally as expected, so I know I'm close.
Any suggestions?
Would appreciate anyone's help on this, it's been really confusing!
In the first place you'll need to make sure that requests made to /index-api.php are not matched and rewritten by the second rule. In the second rule you can use $1. $1 will be replaced with whatever was matched in the first capture group. We'll also need to make sure that the second rule will not match what it rewrites, or we'll end up with an infinite loop and an internal error.
You can use the $1 in the first rule too, as I show below:
RewriteRule ^api/(.*)$ index-api.php?url=$1 [L]
RewriteCond %{REQUEST_URI} !^/site/dist/
RewriteCond %{REQUEST_URI} !^/index-api\.php
RewriteRule (.*)$ site/dist/$1 [L]
I recommend reading the documentation of mod_rewrite to get a better understanding how you can use it and what things you have at your disposal while rewriting url's.
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!
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.