PHP SEO optimized address and physical page address conflict - php

I have a static website that I am planning to move towards php. I will be using php to just load the navigation.htm and main contents page and combine to form a single html file.
Actual Page is here: http://abubakar.zxq.net/html/0007HorizontalAxisWindTurbineMadeWithPVCPipe.htm
How It is accessed through php:
http://abubakar.zxq.net/index.php?page=0007HorizontalAxisWindTurbineMadeWithPVCPipe.htm
I want the page to look like 2 when site is addressed with URL in 1. It is needed to keep the already shared URLs remain working.
Here is how I defined httaccess:
RewriteEngine On
RewriteBase /
RewriteRule ^html/([a-zA-Z0-9_-]+)$ index.php?page=$1
Kindly inform is it possible to do it in present form? I have a solution in mind that old files should be shifted to a new folder and the design php so that it works on old link. Thanks in advance for your kind help.

The regex you wrote doesn't match the first URL.
You need to add the dot character (because the URL ends up with ".htm"), try this :
RewriteRule ^html/([a-zA-Z0-9_\.-]+)$ index.php?page=$1

Related

How do I change the url look with .htaccess but keep the php functionality?

I'm trying to learn, so please be kind. My site is database driven and uses ID numbers to generate the pages. I'd like the URL not to show the ID number but the name of the page. Is there a way to do this where it is just cosmetic and doesn't effect the site? I'm also most likely screwing up the way I'm writing the redirect as it doesn't seem to work at all? What am I doing wrong? Thank you.
This is how my URL looks now:
http://mydomainname.com/index.php?id=35-Entertainment
I'd like it to look like this:
http://mydomainname.com/Entertainment
RewriteEngine On
RewriteRule ^([^/d]+)/?$ index.php?id=$1 [L,QSA]
Unfortunate there is not a way to do this.
The process in this case is the following:
A user enters the a url. In this case you want a nicely formatted URL (http://mydomainname.com/Entertainment)
The user sends this url to your server
Apache processes the url and points it to your project directory based on the domain used in the url
There Apache uses the htaccess to see what to do next. In this case the best method would most likely be to use RewriteRule ^(.*)$ index.php?url=$1 [QSA,L] or something like that
Then you need to do the routing within your app. You can use a routing table for instance to map the nicely formatted slug to an module/id combination
This process can't be done by Apache for it does not known about the content present in your database.
Hopefully this is of any help for you.

Rewrite Old dynamic URL to new Dynamic URL

My website was using the below URL format
localhost/loc-New-Delhi-India (loc - was common in all URLs and the text after it used to change)
Now, I have changed it to
localhost/New-Delhi-India/location (Removed the loc and placed it at the end of URL as 'location'). For this, I'm using the below rewrite rule
RewriteEngine on
RewriteRule ^([A-Za-z0-9-]+)/(location)?$ city-detail.php?cityurl=$1
cityurl grabs the string after 'localhost' and searches the db.
Now since the website is heavily indexed by Google and shared a lot on social media, I want to redirect those urls to the new URLs. I tried the below code, but this just doesn't work.
RewriteEngine on
RewriteRule "loc-(.*)" "^([A-Za-z0-9-]+)/(location)?$ city-detail.php?cityurl=$1" [R]
The new URLs are created successfully, but the old one's fail to redirect to the new one. I'm sure something's wrong but cannot find it. I researched the old questions but they are a bit different. Here, I'm not including any subdirectory after localhost and adding a directory 'localhost' after the city name.
Any help will be appreciated a lott :)
The following would probably work:
RewriteRule loc-(.*) $1/location [R=301,QSA]
RewriteRule ^([A-Za-z0-9-]+)/(location)?$ city-detail.php?cityurl=$1
The first rule will trigger a 301 redirect which will also tell any search engines that your resource has been permanently moved and that would probably make them change their indexes to reflect this. Once you've redirected the 2nd rule should trigger as normal.

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.

Redirect requested asp urls to php using htaccess

We had a major site rework and now our new site is PHP based. We have some sites which have our old pages linked to them which were asp pages in the past. I want to create a htaccess rule but I have failed to get the desired result. If anyone can help it would be great.
The incoming pages will be in the format
http://www.mysite.com/tour.asp?id=1234
where 1234 is dynamic and can vary. I want to redirect this to the page viewer.php and change the query string variable id to tourid so the page requested should be
http://www.mysite.com/viewer/1234
I have another rule in htaccess which routs the above url to
http://www.mysite.com/viewer.php?tourid=1234 (this is working)
I need a rule by which the 1st url is routed to the 2nd one hoping it will open the 3rd url. How can i achieve this?
Since I am new to htaccess, can someone help me around how to achieve this routing? I want to clear here that the server now is php and I have to accomplish this using htaccess only. I followed other similar questions but found none that answers my query.
UPADTE - I got it wrong first time, here's the corrected version.
# Match and capture the id value from the URL
RewriteCond %{QUERY_STRING} id=([0-9]+)
# Redirect by passing the ID to the new site - the ? at the end removes existing query string
RewriteRule tour.asp http://www.mysite.com/viewer/%1? [R=301,L]
I've assumed the IDs are numeric only.
The source for this answer is at https://httpd.apache.org/docs/2.2/mod/mod_rewrite.html - search for "Modifying the Query String" to find the info about removing the query string.

htaccess for dynamic web pages

I have a website and i am using MySQL to store and fetch data from, there is a bunch of data of different destinations (Yes this is a travel agent website) i am wondering how can i setup .htaccess file to display SEO friendly URL
For example: http://www.mywebsite.com/flights-result.php?id=10 this URL is a details page for a flight to Entebbe in Africa, i would like to have the URL for this like http://www.mywebsite.com/Africa/Entebbe.htm
And so on for them, one more thing do i need to add this for every page? the data is being update on daily basis so is there any easy way to write URL automatically?
Any help highly appreciated.
I don't really think what you are trying to accomplish has anything to do with mysql. What you are looking for is called URL rewriting. There are countless number of articles out there that could show you the direction to follow. I am not very sure which web server you are using right not. I presume it is Apache. Here is Apache module_rewrite guide.
Given the original URL, there isn't all the information in there to use mod_rewrite to do this completely.
So what you could do it send all web requests to a controller file, and from there parse the request uri and load the correct page.
So in htaccess, something like...
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ controller.php [L]
Then in controller.php you parse the url and load the correct page.
A different option you may prefer, (if you're flexible on the specific final URL) is to have URLs ending up looking like this:
http://www.mywebsite.com/flights/10/Africa/Entebbe.htm
This would likely be simpler to do instead of implementing a controller (although I prefer the controller for routing requests).
So in htaccess...
RewriteRule
^/flights/([0-9]{1,10})/([a-zA-Z]+)/([a-zA-Z]+)\.htm$
flights-result.php?id=$1&country=$2&place=$3 [L]
Then near the start of the flights-results.php file you should load the data for the id, then check that the provided "country" and "place" are correct (to stop people just entering anything here), and return a 4040 if it's not.
Remember to change all the links your app outputs to the new style as well.
You could also, as you mentioned, hard code all these URLs into a htaccess, but that's not ideal :)

Categories