I have a series of pages on my website which are generated identically using php and a database. The only thing that changes is which topic is selected from the database. I want the user to be able to navigate to the each of these topic pages directly like www.mywebsite.com/<TOPIC> where <TOPIC> is the name of ANY topic. So currently I have a source folder in my public html directory along with an index page for each of these topics. As I'm about to add a lot more "topics" I feel like there is a better way to do this.
What I am looking for is for the user to be able to type www.mywebsite.com/<TOPIC> and a SINGLE php script is used to generate a page based on what <TOPIC> is.
The only thing I could think of was using the 404.php page for this and only showing an error if the topic didn't exist. But with that idea, I don't know how I would get what <TOPIC> and feel like that is a hack.
create or edit your .htaccess file in your document root and write this:
...
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^([A-Za-z0-9-]+)/?$ index.php?page=$1 [NC,L]
</IfModule>
Then you can call a page like:
http://example.com/this-is-my-topic
and in your index you would be able to handle $_GET["page"] with the value of this-is-my-topic.
That is of course only if your server supports Clean URLs and mod_rewrite.c
Related
So, I ran into an issue with query strings and rewriting them to seem "prettier"
My website is at: localhost/admin/ so when uploaded to my server it is also under the /admin directory, and the .htaccess file looks like this:
RewriteEngine On
# redirect URL with empty query string to index.php
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^/?(.*)$ /admin/index.php?$1 [NC,L]
Unfortunately, my design for the admin panel all of the pages are linked to the index.php file. So when I originally used to request http://localhost/admin/?users it would show a list of all of the users. I wanted to make this prettier so I used the above htaccess rules and came up with http://localhost/admin/users. Two issues arose with this:
The css files within the admin folder under css/ could not be accessed so none of the styles were being applied.
This ?users page contains pagination using the GET parameter in php as well, so it looks kind of like http://localhost/admin/users&page=1 and used to look like http://localhost/admin/?users&page=2.
So basically what I wanted to do was make the code look more like this: http://localhost/admin/users/page/1. Unfortunately I cannot hard code the users parameter in because I have other sections like "websites" and I will also have a edit users page with id's etc. So I was wondering if someone could help me with the best way to approach this problem or how I can fix the above htaccess rules? The only thing that kind of helped was: .htaccess mod_rewrite Unknown number of Variables of a GET form but I am still lost on how to implement it for multiple empty GET variables at the front of the url.
Thanks!
Here's what I do,
First for css part use <base href="domain.com/"> in head section of your pages.
You are redirecting full url to query string of index.php and I am assuming you are retrieving is as $_SERVER['QUERY_STRING'];.
So I suggest you that you change your pagination url to http://localhost/admin/users/page/1.
And after that when you retrieve them in index.php explode the string using explode function,
$arr = explode('/',$_SERVER['QUERY_STRING']);
Inside your $arr you will have all your values which you got them from query string.
I am using Windows IIS 7 with PHP and MySQL.
Whenever someone opens an account on my website, he/she will also create one store ID like MyHawaiiSpa. Once this store ID is generated, it can not be changed. Here I have assumed the pkid of this account is 1.
Now, a visitor clicks the MyHawaiiSpa store link from the home page. The visitor will be sent to shop.php where all items belonging to MyHawaiiSpa will be displayed. By clicking on any product on the MyHawaiiSpa store, the product.php page will open, where product details are displayed.
Currently, I have managed it using querystring in PHP. so each store is distinguished like this: mywebsite.com/user/shop.php?sid=1.
My question is now this: I would like visitor clicks on the MyHawaiiSpa store link from the home page, so that the visitor will see mywebsite.com/user/MyHawaiiSpa/shop.php instead of mywebsite.com/user/shop.php?sid=1 (which I have already done now.)
The solution I was thinking of was this: Whenever someone opens an account and creates their store ID, the folder MyHawaiiSpa will be created and the shop.php as well as the product.php pages will be copied under the MyHawaiiSpa folder programmatically. This process will be repeated for all stores those are going to be created.
Is there any other way to do this so I don't need to create a folder and copy files for each store and still I can get desired result I just explained above?
Well it bad idea to copy PHP file when someone registered, it will be mess to maintain it in long term, you just need to store the ID MyHawaiiSpa as unique key in database. and use store id as slug in URL.
since you are using IIS here is good read to rewrite URL
http://www.iis.net/learn/application-frameworks/install-and-configure-php-applications-on-iis/provide-url-rewriting-functionality
You can learn .htaccess mod_rewrite from this link
and then you can covert these rules to IIS7 friendly using this link
You need to use URL rewriting . It is very easy to learn and implement.
On linux servers it is done using a file call .htaccess. On IIS I think file is called
web.config
Basically what this does is, you configure it to show one url while in reality you go to another url.
These links should get you up and running fast
http://www.robbagby.com/php/php-and-iis-running-php-under-fast-cgi-and-url-rewriting/
http://salopek.eu/content/26/introduction-to-url-rewriting-using-iis-url-rewrite-module-and-regular-expressions
https://www.youtube.com/watch?v=guzJWqNJ3DA&feature=channel
http://www.phpgenious.com/2010/04/url-rewriting-with-php-and-iis-7/
http://www.iis.net/learn/application-frameworks/install-and-configure-php-applications-on-iis/provide-url-rewriting-functionality
A good way to do this is an .htaccess rewrite which will allow you to turn your query into an SEO-friendly url and still read properly when called on that page.
Here is apache documentation
A basic example of a rewrite to force all requests through the index page unless they are a real folder or file would be something along the lines of:
RewriteEngine On
RewriteCond %{REQUEST_URI} !(/$|\.|^$)
RewriteRule (.*) %{REQUEST_URI}/ [R=301,L]
RewriteCond $1 !^(index.php|images|robots.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?$1 [NC,QSA,L]
To do a similar rewrite on a windows server you would need to edit a web.config file.
I have a dynamic PHP based site and I've recently noticed its generating a lot of weird pages like this:
http://www.festivalsnap.com/festival/3151748-16th+Annual+Magnolia+Fest+/hotels/3151748-16th+Annual+Magnolia+Fest+/ticket/hotels
The site architecture should be like this www.mysite.com/festival/ and then there are 4 possible child pages for each event... /lineup /tickets /hotels /news
As you can see from the URL it just keeps creating more and more unwanted child pages. When I run a sitemap generator it will just keep going forever and creating more of these pointless pages.
It shouldn't go any deeper than the /hotels page but for some reason its just adding more and more child pages using any combination of the above pages.
I'm no good with PHP and my developer isnt being very helpful. Anyone know what could be causing this?
Edit:
The main event page comes from a file called festival.php and then there are 4 child pages under that - lineup.php tickets.php hotel.php and news.php that get variables from the event page (event title, dates, location, etc) and use it to search for tickets, hotels, etc.
I have noticed that I can tack on basically anything to the URL and it will add it in as part of the page title/event title. It looks like there is something weird going on with .htaccess
Here is the .htaccess code:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.festivalsnap.com$ [NC]
RewriteRule ^(.*)$ http://www.festivalsnap.com/$1 [R=301,L]
RewriteRule festival/(.*)-(.*)/lineup$ lineup.php?eveid=$1&festival=$2
RewriteRule festival/(.*)-(.*)/news$ news.php?eveid=$1&festival=$2
RewriteRule festival/(.*)-(.*)/tickets$ ticket.php?eveid=$1&festival=$2
RewriteRule festival/(.*)-(.*)/hotels$ hotel.php?eveid=$1&festival=$2
RewriteRule festival/(.*)-(.*)/hotels/(.*)$ hotel.php?eveid=$1&festival=$2&hsort=$3
RewriteRule festival/(.*)-(.*)$ event_page.php?eveid=$1&festival=$2
RewriteRule artists/(.*)-(.*)$ artists.php?artid=$1&artname=$2
This is partly something to do with your generator, and partly to do with .htaccess. The .* operator is extremely aggressive, so your .htaccess file says pretty much anything containing festival/ with a hyphen somewhere later in the URL is a valid URL.
But that doesn't explain why your generator is "finding" all of those pages; there must be some bad links being created somewhere, either internally in the generator or in links on pages on your site.
Can you post some code?
EDIT: The .htaccess code should be much narrower - try replacing each of the occurrences of (.*) with ([^/]*).
As for the PHP, it's impossible to say exactly what is going on, but it sounds like the generator is finding those links on your site somewhere and following them, in which case the sitemap generator is working correctly, but your content has problems. Check your logs, find one of the incorrect URLs, and see what page referred the user there. That will tell you where to look for the bad code.
I have been asked to create a website and wanted to design it so that the user may add new blog posts to a news feed. I've tried this and successfully created a very simple blogging feature which adds/reads entries in a sql database.
However I want to dynamically create new webpages for each blog entry that is added by the user. How would I go about this? I am new to Php/Sql and so I am unsure of where to start. Any pointers in the right direction would be greatly appreciated.
As for an example of what im after, see: http://kingslandprimaryschool.co.uk/
Thanks
The solution is to hire an army of employees, who manually create hundreds of scripts. A lot of work, but what else can you do? Nah, just kidding.
The trick is not to create a new file for every blog page, you just have to change the routing. Start by creating a blogging system which works through consistent URL's, such as blog.php?p=home. When you've finished, create a .htaccess file which routes certain requests to that page. For example, if you would want an URL like my.site/home to load blog.php?p=home you would need the following .htaccess script:
<IfModule mod_rewrite.c>
RewriteEngine On
# Allow existing directories and files
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
# Route the request
RewriteRule ^(.*)$ blog.php\?p=$1 [QSA]
</IfModule>
This would first check whether a file exists at the requested URL. If it does, it shows that file. Otherwise, blog.php?p=myPath is opened. The [QSA] tag after the redirect means that everything after the question mark is passed on to the script you redirect to, so if you would go to my.site/home?day=wednesday the page blog.php?p=home&day=wednesday would be loaded.
If you need more help, feel free to ask any question. Alternatively, you can just Google "htaccess mod_rewrite" or "PHP pretty url" for more information. Hope this was of help!
I am building a website and I will need to make it SEO friendly so, as it is now, I am using a dynamic website (PHP) and through .htaccess, I am making it appear as if the site has static pages.
To do this, I am redirecting to a php file which then displays the content.
The url looks like: www.mainpage.dk/phpfile-navigationvalue-value.htm
I am using a navigation value inside the page to render it according to which menu item is clicked.
The guy I am building this for says that a url like www.mainpage/something.html is better for SEO purporses than www.mainpage.dk/phpfile-navigationvalue-value.htm. Can anyone come with some input on this matter?
And if the regular static page is better, is there a way to make a dynamic look just like a regular static page?
PS: The reason why I want a dynamic page is that the page is going to be extended with new pages every now and then as well as updated frequently.
To make it look like a regular page you could add *.html -> alias.php?alias=*
Then check the aliases and display the proper page from PHP.
Also, how about making it :
www.mainpage.dk/phpfile/navvalue/value/ -> index.php?page=phpfile&nav=navvalue&val=value
I would discourage redirecting to a phpfile, but handle it via index.php?page=* (look line above), or something similar.
Edit:
how htaccess should look
RewriteEngine on
# [!]for `*.html` -> `alias.php?alias=*`
RewriteRule ^(.*)\.html$ alias.php?alias=$1 [NC]
# [!]for `/phpfile/navvalue/value/` -> `index.php?page=phpfile&nav=navvalue&val=value`
RewriteRule ^(.*)/(.*)/(.*)(/)?$ index.php?page=$1&nav=$2&val=$3 [NC]