How to load Hard Coded Pages with CMS values using PHP? - php

Let me explain the scenario first.
I have built a cms in which I load every page on one page like this.
http://www.mywebsite.com/index.php?page=about
Here "about" is a slug of the page I create from the Cms.
So the sql query fetch the record using the slug & retrieve the content of the page & print it on index.php
Now the next thing I have done is, I have done URL rewriting in .htaccess file like below
Options +FollowSymlinks RewriteEngine On
RewriteCond %{REQUEST_URI} !^.(images/|.js|.css).$ [NC]
RewriteRule ^([a-zA-Z0-9_-]+)$ /index.php?page=$1
So now I can use this kind of URLs on my site for same "about" page
http://www.mywebsite.com/about
Now the Question is, these all pages are simply content based, but how should I maintain Dynamic pages.
E.g. I have a page for Member Registration as signup.php
I want to assign all the title, heading, meta desc, meta keywords from CMS to this page but also want to load this page using
http://www.mywebsite.com/signup.php
& not like
http://www.mywebsite.com/signup
What kind of logic should I apply to load the page as signup.php & also fetch the CMS values ?
One solution I have in mind is, we should put one more field in CMS as "Code File :" & enter signup.php over there. But still I am not sure.

Add another rewrite condition which forces it to not redirect to index.php:
RewriteCond %{REQUEST_URI} !\.php$ [NC]

Related

make laravel 5 generate seo friendly url after submit

I am using laravel 5.2 and I have search form which pass GET parameter, so in url its like
http://www.example.com/list/item?&qs=&unit-type=Office&min-bed=&max-bed=&min-price=&max-price=
so search result is shown with such ugly url, I want to make this url seo friendly, I don't want to use post method.
Is there any .htaccess code that can help to make seo friendly url of such get parameter.
I tried this .htaccess code but doesn't work.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?params=$1 [L,QSA]
What you are doing is possible, but shouldn't be done.
The page is not a static page, or a product page, or a listing page. It is a search result page.
The search result page should not be SEO friendly in terms of the url containing information about the search query because this is supposed to be dynamic. A search engine will not be interested in indexing a specific user's search result page on your website. A search engine will be interested in indexing the search page.
If you are looking to have a product listing page for example with pre-defined search queries, then why not just create a specific controller and for the time being, manually write an SEO friendly url to point to the required method on that controller?
e.g.
Route::get('offices-between-10000-and-20000-pounds', 'ListingController#getOfficesBetweenPriceRange');

Mod rewrite - PHP

I use urls like
www.example.com/viewpost.php?TITLE=mp3playerforsale&ID=123
Where i am passing ID parameter and Title and then display records from sql in PHP.
Later I can able to use MOD REWRITE functionality in .htaccess and able to convert as dynamic seo friendly url like
www.example.com/mp3playerforsale-123
www.example.com/carforsale-124
Where 123 gets corresponding records stored in sql database, e.g.
ID Title
123 mp3playerforsale
124 carforsale
Now I have the requirement that I have two pages which have ID and title as parameters. But both has to displayed in two different pages.
Example:
mp3players should go to page viewpost.php?ID=123
cars should go to page viewpost2.php?ID=123
When i tried both displayed in viewpost.php where I need to display cars category in different pages.
I am already using this
RewriteEngine On
RewriteRule ([-A-Za-z0-9]+)-([0-9]+)/? viewpost.php?TITLE=$1&ID=$2 [NC]
Any help appreciated !
You may redirect both cases to the single page, where it will be decided which template to use by php code.
I'm not sure i understand your question i hope these will help you
RewriteEngine On
RewriteRule mp3players-([0-9]+)/? viewpost.php?TITLE=mp3players&ID=$1 [NC]
RewriteRule cars-([0-9]+)/? viewpost2.php?TITLE=cars&ID=$1 [NC]
Or this?
RewriteEngine On
RewriteRule mp3players/([-A-Za-z0-9]+)-([0-9]+)/? viewpost.php?TITLE=$1&ID=$2 [NC]
RewriteRule cars/([-A-Za-z0-9]+)-([0-9]+)/? viewpost2.php?TITLE=$1&ID=$2 [NC]
in this your links must have a mp3players or cars prefix
like
www.example.com/mp3players/nameofproduct-123
www.example.com/cars/nameofproduct-124

PHP Site Generating Strange Pages

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.

Multiple htaccess re-write rules with friendly URLs

My mobile site (tiny custom CMS inspired by wordpress) has a homepage, blog page, a news page, an about page and a contact page where ./blog ./news ./about and ./contact are actual folders. Homepage, Blog and News are paginated. About and Contact are single pages, no pagination.
I need htaccess rewrite rules to do the following (A) and (B). I have only made progress with (A), and the htaccess file is in the site root.
(A)
m.website.com/index.php?page=1 re-writes to m.website.com
m.website.com/index.php?page=2 re-writes to m.website.com/page/2 etc
m.website.com/blog/index.php?page=1 rewrites to m.website.com/blog
m.website.com/blog/index.php?page=2 re-writes to m.website.com/blog/page/2 etc
m.website.com/news/index.php?page=1 rewrites to m.website.com/news
m.website.com/news/index.php?page=2 re-writes to m.website.com/news/page/2 etc
Problems:
Below is what I'm using for the above, but I only got it working for the homepage for now. I don't know how to combine the rules to include blog and news pages too. Also, it duplicates my links because m.website.com and m.website.com/page/1 are both in use. I need to get rid of /page/1 everywhere. Pagination should start only from page 2. I tried to get rid of it using the RedirectMatch but it didn't work so I commented it out.
RewriteEngine On
RewriteBase /
RewriteRule ^page/(.*) index.php?page=$1 [NC]
#RedirectMatch 301 ^/page/1/$ http://m.website.com/
(B)
I already have a permalink.php file which accepts pretty URLs and returns their postIDs
The read-more link for each article on the home, blog or news page will have the format http://m.website.com/2012/03/the-post-title
When clicked, the htaccess will query permalink.php with the string /2012/03/the-post-title to get the postID, then opens http://m.website.com/article.php?id=postID but the address in the address bar will be http://m.website.com/2012/03/the-post-title and this shows the article in full, be it home page, blog page or news page.
Problem: I have been searching and I'm not exactly sure how to go about (B) above but I know it's possible. In the end, all rules for A and B will be in the same htaccess file in the site root.
Thanks
This should do the trick for you. It looks like you have done most of it so there isn't muc to do.
RewriteEngine On
RewriteBase /
# you do not need a rewrite for the root as you should have index.php
# as your default document and page 1 as your default page
RewriteRule ^page/1/?$ / [NC,L,R=301]
RewriteRule ^page/(\d+)/?$ index.php?page=$1 [NC,L]
# you do not need a rewrite for blog as you should have index.php as your
# default document and page 1 as your default page
# you do not need a rewrite for news as you should have index.php as your
# default document and page 1 as your default page
RewriteRule ^(blog|news)/page/1/?$ $1 [NC,L,R=301]
RewriteRule ^(blog|news)/page/(\d+)/?$ $1/index.php?page=$2 [NC,L]
######################################################################
################## ADD YOUR PERMALINK.PHP CODE HERE ##################
######################################################################
UPDATE
To effectively turn /2012/03/the-post-title into a postID you need to be able to ask your database to do that for you as it is the only thing that knows the answer. So you can either use a RewriteMap http://httpd.apache.org/docs/2.3/rewrite/rewritemap.html#dbd I have never done this myself and I would advise against it. Not because I know there is a problem with it I just have a bad feeling about it.
The alternative and one I would champion is to just do something like this:-
RewriteRule ^(\d{4})/(\d{2})/([^/]+))/?$ article.php?postIdentifier=$1/$2/$3 [L]
Then in article.php hit the database and use the information in the postIdentifier to get the correct article.
Make sense?

Redirecting to main page (index)

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]

Categories