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.)
Related
I have been looking for several solutions on the web but as I have always had problems interpreting the htaccess, I have not achieved what I wanted.
I have a website with the following type of link:
https://sub.example.com/search.php?gosearch=new+music+techno
https://sub.example.com/search.php?gosearch=nice-music-rock
And I would like to obtain a link in this way:
https://sub.example.com/search/new-music-techno
I also have a problem, when in my form I press enter, this automatically adds the "+" symbol to all the spaces and I do not know with what method the change would be made automatically to a "-", if with PHP, with JQuery or with .htacess.
In parallel I also have this type of links:
https://sub.example.com/topmusic.php
And I would like to obtain this type of link:
https://sub.example.com/topmusic/
Is it possible to implement everything with htacess?
Do I need to make several conditions or just one?
Is it possible that old urls are redirected automatically to the new form once the user clicks on them?
You can use like this
RewriteRule ^search/(.*)$ search.php?gosearch=$1 [NC,L,QSA]
And on search.php you need to replace the code. htaccess will send the whole string next to "search/" on search.php file under gosearch $_GET parameter.
With help on my last question about removing file extensions from the address bar if the page name and file extension were keyed in directly even after using htaccess to remove them, HERE I'm now wanting to remove index from the url. Specifically, if I type in my webpage's URL into a browser like so: http://webpage.com that exact address is what I see in the browser when the page is served up. However, if I click the Home link on my page, which contains a href="index" naturally, what shows in the address bar is http://website.com/index Certainly, I could change the link in my code to a href="http://website.com" to omit the index portion from showing up in the address bar, but that seems like a hack.
I found an SO article HERE that shows a method for removing index.php but I'm a bit lost as to how, or even if the code in that answer (I get particularly stuck when seeing Rewrite Base /
) can be blended in with the code that was given to me HERE to just remove index after the file extensions have already been removed.
My apologies in advance if this isn't a clear explanation, I'm in new territory here. Many thanks in advance!
Doing proper rewriting requires you to build the correct links that you want to see being used by the web (i.e. users and search engines). Don't use URLs in your own links that point to URLs that you consider to be wrong.
The link to the home page likely should be <a href="/">, if you don't like index.
Trying to fix the problem by making the client do two requests, one for index, then receiving a redirect to /, is the hack here. Fix your links - they are under your control for a reason.
Certainly, I could change the link in my code to a href="http://website.com" to omit the index portion from showing up in the address bar, but that seems like a hack.
No, that's what I would do :-)
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]
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 /
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