For last 4 days I´ve been trying to make a friendly url using .htaccess (mod_rewrite)
I have a some news on frontpage and link redirects to the full article on news_id.php
So mypage.com/local/news_id.php?newsid=37 should take the headline from the "headline" field
in phpmyadmin - for example mypage.com/local/police-stops-girl-fight-at-the-mall
my .htaccess code is
RewriteEngine on
RewriteRule ^local/([^/.]+)/?$/local/news_id.php?headline=$1 [L]
Some tutorials says I also have to edit the php link on frontpage which is
<?php echo $row ['headline']?
...but I've also stumbled on articles that says that I don´t need it and show this should do
it.
I've checked out if mod_rewrite is enabled at the server and it works fine.
Am I close or is this much more complicated than I think?
Previously you were fetching by id. Now you are attempting to fetch by title.
That's a fundamental difference in the way your article lookups will need to occur and what content will be in your urls.
It may be easier to start out with a simple experiment site. Get that working. Then convert your real site over once you've seen how it all works.
Related
I am having some issues with my website indexing. The thing is that I have a Website of news using PHP and MySQL. Every time that I create a news post a URL like this link the post.
Example:
//pretty URL
//nameOfSite + PostId + newsTitle
mYpagExample.com/5/the-obama-lagacy
Google is indexing this 2 ways
mYpagExample.com/5/ // Ii dont want this to be indexed
mYpagExample.com/5/the-obama-lagacy
Even though I am declaring the original URL as canonical it seems to be no enough.
<link rel="canonical" href="http://mYpagExample.com/5/the-obama-lagacy">
NOTE: I am using .htaccess for the pretty URLs.
Can someone tell me how I can fix this?
Edited:
this is the link of the post that comes from the DB example:
<?php echo $title?>
.htaccess file
RewriteEngine on
# Get the request URL by the user
RewriteRule ^([a-zA-Z0-9/]+)$ index.php?view=$1**
I'm building a simple site that will only have a homepage and a contact page and wondered if I could use .htaccess to rewrite the urls for different companies.
So for example if I go to website.com/companyname-contact/ it will display that url in the browser bar but actually load a generic contact.php page, I can then use php to pull in the correct contact details for that specific companyname.
This would need to work for different company names (e.g. website.com/anothercompany-contact/) but only work for an array of approved company names.
I realise this may not be possible but I thought I'd ask because i spent about 4 hours this morning Googleing it with no real progress.
Thanks
Unless you want to manually list the approved company names in your .htaccess file (which looks UGLY) I'd suggest this:
RewriteEngine On
RewriteRule (.*)-contact$ /contact.php?company_name=$1 [L,QSA,NC]
and then in your contact.php
determine if valid company name - check db or whatever method you are using. (Make sure to escape the input)
if not valid you have a couple options:
redir to your default 404 page
issue an intelligent warning page (ie include suggestions for alternate spelling that is in the db) and set a 404 header. (better IMO)
if similar company name in the db possibly redirect to that with a note at the top of the page
Yes you can. You need to enable the rewrite engine, and then you will be able to use regular expressions to accomplish what you're trying to do.
This is an example of what your htaccess could like:
RewriteEngine On
RewriteRule ^contact/([A-Za-z0-9-]+)/?$ contact.php?company=$1 [NC,L]
My site has pages like news.php, vidoes.php, products.php etc
The data for these pages are held in a MySQL database they have id, name, description and permalink
If a name is News Article Title or Boxing Match Video they permalinks would be news-article-title and boxing-match-video
Now on the news.php page there is a list of news articles and they are linked to read-news.php with ?id= and the article id beside it.
So a link looks like this www.mysite.com/read-news.php?id=2
On my read-news page I use a GET to get the id and then data for that article comes too.
How do change the links on the news.php page to come up as www.mysite.com/read-news/news-article-title.php and the URL to display the same when reading it.
I cant seem to find a tutorial that shows exactly what I need.
Any help please
Thank you
You have to use a .htaccess file (if you are on a Apache server and do not have access to the server itself, like in a shared hosting situation)
.htaccess is a simple textfile you make and place in the directory in which it controls the access, e.g. the redirection of URL's and has a lot of usable functionality.
With this you can have your URL's rewrited without the user noticing it.
Good for SEO purposes.
More info: http://httpd.apache.org/docs/current/mod/mod_rewrite.html
If you want a quick try, automatic generation is done at
http://www.webconfs.com/url-rewriting-tool.php
I Have a .htaccess file for a blog script which currently generates a number for a blog post based on it's ID in the MySQL database. Is there any way I can change this so that it uses something like $subject as the URL from my PHP file? I have pasted my current .htaccess code below.
RewriteEngine On
RewriteRule ^blog-([0-9]+).html$ index.php?act=blog&id=$1
I hope people can understand what I am trying to describe.
Thanks in advance.
Regards,
Callum Whyte
Using just the subject is probably a bad idea, if you ever change the title, all links to your page (from google, or elswhere) will be broken. I would include both the subject and the idea, that way you'll always be able to match an URL to a blog post:
RewriteEngine On RewriteRule ^blog-[^-]+-([0-9]+).html$ index.php?act=blog&id=$1
This should match everything except a - between blog and the id.
Now you should be able to call, e.g., *yousite/blog-some_title_or_other-494.html* and it will call index.php?act=blog&id=494. This new rule shouldn't break any of your old backlinks either. If it does, let me know and I'll fix it.
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