i got a question abount seo friendly urls.
Manly, i would like to know whats the best way to get my system running with seo friendly urls. i have programmed my own cms system which is working at the moment with url like this:
/content.php?contentid=ID
On every content page you can change the Meta-Tags.
Every LINK which is generated in my CMS follows this structure. If i want to use SEO friendly URLs changing every link, or code, where a link is generated, might be a bad idea. So i am looking for a nicer way. Is there any commonly used way to handle this?
My first idea was to automatically generate a new .htacces file with ModRewrite after the MetaTags of a Contentfile have been changed. So the SEO friendly URLs are recognized by the system. To get the LINKs on every page working i was thinking about checking the current url and redirecting to the new seo friendly url at the beginning of the content.php file.
I dont know if this is the right way to work with seo friendly urls. How are other CMS Systems handling this issue? I would be thankful for any inspiration!
if you are using an framework(which i think you are) then you can use a .htaccess like this to send the new cleaner links in an $_GET to the framework:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
Related
My site is just index.php and every page is read from that. I'm not using WordPress.
What I'm seeking to do is the same thing that WordPress does with its page structure. In Wordpress you can set the rewrite rule so that a subsequent subdirectory can pass a value to index.php.
I have a client that is building an affiliate network and the base URL is for example:
mywebsite.com/?id=viktor
They would like to parse URL's instead so that it's much simpler for the customers.
i.e. mywebsite.com/u/viktor would 'rewrite' to mywebsite.com/?id=viktor.
What would be the .htaccess rule or associated PHP I'd have to put in index.php?
Obviously we won't have a 'u' folder in the site. :)
Thanks for your help.
This related question I believe has the code we're looking for however I'm not sure how it would apply in this situation
You can do it like this:
RewriteEngine On
RewriteRule ^u/(.+)$ /?id=$1 [NE,L]
If you need to also preserve any existing query string (like /u/viktor?param=value), use this:
RewriteEngine On
RewriteRule ^u/(.+)$ /?id=$1 [NE,QSA,L]
Update
Well it's bad practice using relative URLs (for this reason), but if you really want to avoid updating all your code, which could easily be done with a script or a command, you could do this:
RewriteEngine On
RewriteRule ^u/(.+/.+) /$1 [NE,L]
RewriteRule ^u/(.+)$ /?id=$1 [NE,QSA,L]
Which would rewrite everything that is referenced in a folder under /u/ to root. But it breaks browser caching as all your resources now have two URLs. Really you should just update your URLs to absolute.
If the above doesn't work for you and you only want to rewrite specific folders, do it like this:
RewriteEngine On
RewriteRule ^u/((?:assets|images|css)/.+) /$1 [NE,L]
RewriteRule ^u/(.+)$ /?id=$1 [NE,QSA,L]
Replacing your folder names as appropriate.
The problem you have here, Viktor, is that WordPress checks the original URL when it is processing a request. This is because it has its own mod_rewrite rules to rewrite everything to index.php.
So you can't do what you want with mod_rewrite, because WordPress won't look at your rewritten URL, and will process it as /u/viktor. You can do it with a 301 redirect, but you already said you don't want to do that.
In order to make this work properly, you need to work with WordPress to make those URLs work, perhaps creating a plug-in to do it. I wouldn't suggest adding anything to WordPress's index.php as it will only get overwritten when WordPress is updated.
I've made my php website url fiendly with url re-writting as,
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^[A-Za-z0-9-_]+/([A-Za-z0-9-_]+)/?$ product.php?uni_id=$1 [NC,L] #
So, Old urls were,
http://example.com/product.php&uni_id=laptop-123
and new url is,
http://example.com/products/laptop-123/
Now, When search engine comes back they should informed about (old url is permenently moved to) new url or i can remove all entries from search engines in order to get rid of duplicate entries in search engines.
Now, what i have to do after writing url in terms of seo purpose.
NOTE : I've heard about using 301 redirects but don't know how to implement it.
To improve your SEO you should add canonical URLs.
You do that by adding a canonical url in the head of the page in question, so in your example:
<link rel="canonical" href="http://example.com/products/laptop-123/">
That way for example Google knows which url to index and it will not penalize you for duplicate content.
Note that this is only one of the (easiest...) ways to achieve this, you could for example also do a 301 redirect when a visitor is not using your preferred url.
Also see the information from Google itself.
I have a url like this
post_article.php?url=how-to-use-mod-rewrite
I am using mod_rewrite to make it
post/how-to-use-mod-rewrite
Code I am using in .htaccess
RewriteEngine On
RewriteRule ^post/([^/]+)/?$ post_article.php?url=$1 [L]
Now both of this urls will give the same content, which is not good in SEO point of view.
How I can redirect all those old urls to new url.
ie post_article.php?url=any-value to post/anyvalue
Use canonical URLs to help search engines understand duplicate content, if SEO is your sole concern.
RewriteEngine On
Redirect ^post_article.php?url=.*$ post/$1
RewriteRule ^post/([^/]+)/?$ post_article.php?url=$1 [L]
Try it.
Just create a function to rewrite all your links in your articles, and search engines will never be able to find the old url scheme.
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 :)
I have some PHP and HTML in the same file, and I am not exactly sure how to make the URL appear as the name of that page.
Here is an example of what I would like to do: lets say some page id = 1 and the title of that page is HelloWorld.
Instead of site.com/page.php?id=1 I would like the url to appear as site.com/HelloWorld
But the problem I am having is that I only get to know the title of the page inside that page after I query for the title by id.
Considering the setup I described, is there a way to make the urls appear as the names of the pages? And also, if someone links to that page by using the better looking url with the name of the page instead of the id, is there still a way to get the id and by that, the rest of the page contents?
Thanks!!
What you need is to learn more on how .htaccess work.
Here is a good link that got me started:
.htaccess tricks and tips
Update:
Here is a very common practice in many framework where all requests are sent to index.php, and from there you use php to serve the correct page:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ http://www.site.com/index.php [L,R=301]
</IfModule>
This is actually a function of your HTTP server software (often Apache), not of PHP.
What you're seeing happen on sites with "friendly" URLs is that the friendly name is captured in a Regular Expression and then passed to PHP.
For example:
GET /HelloWorld
is sent to your web server.. the web server parses it
RewriteCond ^(A REGULAR EXPRESSION TO CAPTURE YOUR FRIENDLY NAME)$
RewriteRule page.php?id=(EXPRESSION FROM ABOVE)
In this way your PHP script will always receive the friendly name as a parameter.
Take a look at "mod_write" for Apache - which you can often create rules for using an ".htaccess" file in the root directory.