Working with a website that has a large quantity of hard coded pages. The problem is I've been requested to create duplicates of this site with different logos/contacts/addresses. So my thinking was to create URL with the location in it. This location in URL would then be "read" causing the PHP to make changes to specific details.
So URLs would look like:
www.domain.com/repairs-LOCATION/any file
Where as the actual path on the server would be:
www.domain.com/repairs/any file
Complete novice when it comes to htaccess and regular expressions. Initially I tried this, attaching the location name to a file:
# Rewrite repairs page URLs
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^repairs/([^\.]+)-repairs?$ repairs/index.php?in=$1 [L,NC,QSA]
RewriteRule ^repairs repairs/index.php [L]
But this was restrictive as ALL calls to the repairs folder were redirected to index.php, so thought renaming the folder would be a better way forward as a user would be able to navigate to other files within the directory.
Tried a redirect, but that defeated the object as the location was removed from the URL:
RewriteRule ^repairs-townname/(.*)$ /repairs/$1 [R=301,NC,L]
Lastly tried re-jigging the initial attempt, but with no joy.
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^repairs-([^\.]+)/?$ repairs/?in=$1 [L,NC,QSA]
At the moment though struggling to find a solution.
Any help or pointers greatly appreciated, thanks for your time.
Related
I have a site with files contained in a /pages/ dir.
I'm trying to hide the /pages/ directory from URLs but still have them in the directory, so basically I am trying to achieve this:
www.example.com/sub/
should load:
www.example.com/pages/sub/
or
www.example.com/sub/file.php
should load:
www.example.com/pages/sub/file.php
-- the aim here is to remove /pages/ from all urls
thanks in advance
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ pages/$1 [QSA,L]
If file or directory doesn't exist, then try in pages folder.
QSA adds url parameters if any (like ?param1=val1¶m2=val2)
Apache Re-write: http://httpd.apache.org/docs/2.0/misc/rewriteguide.html
Learn it.
Live it.
Hate it?
edit: its a first in first evaluated, last in last evaluated process, so be considerate when specifying rules, as even if one is established correctly, a broader, more general one which also meets the same pattern (depending on wildcards) has the chance to usurp the prior ( think CSS declaration squashing, if you're familiar with that )
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!
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!
I have a site which I have converted to use cms made simple. It works perfectly and I have the friendly urls working fine too. My issue is with a little script I wrote myself and how best to integrate it. The script is a gallery script, it reads a directory and outputs a formatted gallery in html. I was planning on making it a user defined tag in cms made simple but I hit a small snag.
The gallery script needs to be able to read in two values from the url groupId and showpage.
If I am using freindly urls then the cms and use the tag I hit a snag as the cms tries to find an actual page at "www.mysite.com/gallery/mygroup/2" and then throws a 404.
basically I need
http://www.mysite.com/gallery/photogroup/2
rewritten to
http://www.mysite.com/gallery.php?groupId=photogroup&showpage=2
UPDATE
Follwoing Yuri's advice I added his rule to the htaccess. But I have hit another snag.
So for instance if we go to
http://www.mysite.com/gallery/photogroup/2
then Yuri's rule should take effect. But that path is also a correct physical directory on my site coincidentally. Is there a way to have the rewrite rule take effect instead of bringing me to a white screen browsing the files in the directory or to the forbidden screen if I have indexes turned off which I do.
Below is my htaccess
php_flag magic_quotes_gpc Off
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1
RewriteRule ^gallery/(\w+)/(\d+)$ gallery.php?groupId=$1&showpage=$2 [QSA,L]
So, did you try to write in .htaccess something like this?
RewriteEngine On
RewriteCond ${REQUEST_FILENAME} !-f
RewriteCond ${REQUEST_FILENAME} !-d
RewriteRule ^gallery/(\w+)/(\d+)$ gallery.php?groupId=$1&showpage=$2 [QSA,L]
sounds like a "module" to me. Maybe this Make your module use clean URLs