I'm using a new no-name framework that I'm not completely familiar with and I'm hung up on how I will add dynamic content from the database in this environment.
In the index controller, I'm forced to have an index function that answers all requests for index.html. If I wanted to create a second page dynamically from a data source, how do I go about this without having to add a controller for this new page? All requests are currently being routed by htaccess. Without getting into code snippets, I'm simply looking for the logic on how this is generally handled.
Okay, the question is a bit vague, so the answer might seem a little vague too.
Let's have an example to help visualise. You have a newspaper website, so you serve lots of articles - but all from one dynamic page...
/articles.do?Article=Important-News-Item
/articles.do?Article=Cash-Strapped-Business-Goes-Into-Administration
And so on.
You then add some .htaccess (mod_rewrite) love to the site...
/Articles/Important-News-Item/
/Articles/Cash-Strapped-Business-Goes-Into-Administration/
And you rewrite those URIs back to the articles.do page.
So in your articles.do page, you use the request string (Article=Value) to query the Article table and get back the relevant page...
SELECT
Headline,
Content,
Image
FROM
Article
WHERE
RestfulId = 'Important-News-Item'
And then you pop that onto the page.
Shout if I've missed the point on this question.
mod_rewrite example
This is the mod_rewrite rule for the example above, it maps the restful URI back to the articles.do URI.
RewriteEngine on
RewriteRule ^Articles/([^/\.]+)/?$ articles.do?Article=$1 [L,NC,QSA]
The easiest way to explain this (not the technical way to explain this) is:
1) The first bit (^Articles/) looks for any address that starts with "Articles/"
2) The second bit (([^/.]+)/) represents "Whatever comes between the "/" after Articles and the next "/" after that, which in this example is the title of the article.
You can capture more parameters by adding more "([^/.]+)/" segments to the end...
RewriteEngine on
RewriteRule ^Articles/([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ articles.do?Article=$1&$2=$3 [L,NC,QSA]
In this example you could convert
/Articles/Important-News-Item/My-Key/My-Value/
into
articles.do?Article=Important-News-Item&My-Key=My-Value
Related
I don't even know how I could title this question but here is the problem:
I use seo friendly urls on this project where there are services. Service links look like; .com/services/service-title-6 and they get translated to; .com/services.php?s=service-title-6. On the other hand, pages don't have the .php extension at the end of their urls like: .com/services or .com/about-us.
The actual problem comes when I'm handling the switch between multiple display languages for this website. I just used a get parameter called lang and I can pass it to any page since I have a php file I call on every page to handle this along with other stuff. On my navigation bar, the language change button links are created dynamically so that the user doesn't have to leave the current page to change the website's language. So if you are viewing the about page, the link is: .com/about-us?lang=en to switch to English. This is generated in php like $current_url . "?lang=en".
However, when I'm viewing a page with parameters such as a service, the link gets translated by .htaccess to something like: .com/services.php?s=service-title-6?lang=en. As you can tell, the 2nd question mark will get ignored as a parameter and not be passed. I would need to use an ampersand instead of the question mark. But when I'm just doing this on a regular page, I can't use ampersand by itself as it will be translated to: .com/about-us.php&lang=en.
Sorry for the long question but any help and ideas to handle this problem would be appreciated. Thanks.
I fixed my problem by appending the next query string to my url. I wasn't aware of the QSA flag of mod_rewrite.
Here is my original rule for the services page:
RewriteRule ^services/(.*?)/?$ services.php?s=$1 [L]
I replaced it with:
RewriteRule ^services/(.*?)/?$ services.php?s=$1&lang=$2 [L,QSA]
I don't know if it's 100% reliable but it seems to work perfectly for what I need, even after doing a few tests.
(Thanks to Joseph Sible-Reinstate Monica for directing me to a helpful question.)
I need to do this using htaccess
When a request is made for http://www.example.com/home, it should (internally)load the page http://www.example.com/home_st.php
Similarly when a request is made to other link called products (http://www.example.com/products), it should (internally)load http://www.example.com/products_st.php
In short what it is doing is appending "_st.php" and loading that URL. But one thing I do not want is if the user directly types http://www.example.com/home_st.php or http://www.example.com/products_st.php in the browser, it should show 404 / Page not found error
I have few other pages in that folder and I want those pages to behave in this manner. I understand the htaccess should have something like this
Turn on the rewrite
Forbid access if the URL is called with page names like home_st.php, products_st.php etc.
If it's "home" or "products", then rewrite(append?) it to home_st.php and products_st.php respectively. I have other files too while need to follow the same
P.N: My URL should not show the actual filename, for example home_st.php, products_st.php etc. It should only show as http://www.example.com/home, http://www.example.com/products etc
htaccess and regex is not something that I am well acquainted with. Any help would be great. Thanks
You want to be able to re-write URL's
This has been written before but i'll say it again.
You want to use the Htaccess file and the re-write rule.
RewriteEngine On # Turn on the rewriting engine
RewriteRule ^pet-care/?$ pet_care_info_01_02_2008.php [NC,L] # Handle requests for "pet-care"
This will make this url: http://www.pets.com/pet_care_info_07_07_2008.php
Look like this: http://www.pets.com/pet-care/
Links to more information: How to make Clean URLs
and for the webpage I used to reference this information from: https://www.addedbytes.com/articles/for-beginners/url-rewriting-for-beginners/
To get your pages set the way you want them, I would advise that you read the two articals and try get what you are looking for.
If you need some specific help with doing this, ask.
Hope that helps.
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.
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.
In my site, I have used mod rewrite to make search engine and user friendly urls.
Only 3 rules:
RewriteRule ^articles/([a-z]+)/([0-9]+)/?$ /index.php?page=articles&cat=$1&id=$2 [L]
RewriteRule ^articles/([a-z]+)/?$ /index.php?page=articles&cat=$1 [L]
RewriteRule ^([a-z]+)/?$ /index.php?page=$1 [L]
But index.php is still accessible by anyone and will work even if a friendly URL is not used(that is, instead parameters are passed).
So, does this down rank by search engine ? Do I have to block direct access to files with .php extension ?
If you have 2 URLs that load the same page where one is search engine friendly and the other is not, this is not really detrimental to your site AFAIK. Basically you just want to expose to search engines as much as you can, so if you need to provide a parallel track, for example an anchor tag that works fine without Javascript because the action will take you to the correct place (which is ideal for a bot) but typically is managed by Javascript for clients that have it (most standard web browsers) then you're golden.
EDIT:
Per OP question in a comment about parallel paths.. Say I have a link, an anchor tag.
<a id="moxune_services" href="http://moxune.com/services" action="get" target="_self">Moxune Services</a>
You can see that this is a valid link (and I will be getting SEO points for it from StackOverflow ;P But anyway, say this is part of a heave JS driven site, and rather than refreshing the whole page when this link is clicked, I just want to have a subsection of the page like where a div w/ id="content" is present be replaced by the fresh content after I have AJAX load it. The js would be something like this (w/o testing, this is just off the top of my head) (a jQuery solution as well):
// very crude jQuery example!
$('#moxune_services').click(function() {
$.get($(this).attr('href'), function(sNewHtml) {
$('#content').replaceWith(sNewHtml);
});
});
Now you see, the google bot can reach the page through the HTML a tag, no problem, but your customers looking for a Web 2.0 (TM) website will be able to enjoy the lack of full page refreshes as they have JS enabled (and hopefully aren't using IE 6 :O).
One term for this is 'graceful degradation'.
quickshiftin is right here. There's no point hiding the index.php
If you must, however do this:
RewriteRule ^index\.php.+$ / [L,R=301]
I didn't test this, so it might not work, but the general idea is to redirect index.php to /