I've had a real tough time trying to search for the exact htaccess code that will allow me to do the following:
Visiting: http://www.domain.com/wildcard
Should show: http://www.domain.com/
But the URL should still read: http://www.domain.com/wildcard
So basically a transparent redirection... seems fairly straight-forward, but surprisingly hard to search for. The PHP in index.php is already set up to parse the subdirectory and read it as a parameter, but unfortunately my client never supplied me with the .htaccess file. #developerproblems
Thanks!
You just need this ErrorDocument 404 line at top of your .htaccess:
ErrorDocument 404 /
This will show home page for any request that is not a file or directory and results in a 404.
We will suppose that you have to receive a url parameter called param so your rewrite rule should be:
RewriteEngine On
RewriteRule ^([^/]*)$ /?param=$1 [L]
By this way any http://www.domain.com/AnythingHere will render the contents of http://www.domain.com/?param=AnythingHere so the home page is rendered.
However, such solutions, they are not change the contents, they may leads to SEO problems for repeated contents, So the solution of anubhava is better for SEO according to your usage.
Related
I created a 404.php page for a website. Also, there is the .htaccess file ( in the /root ) having the ErrorDocument 404 /404.php line.
The website is having a multi-language functionality, something like this:
sitename.com/it/article1
sitename.com/en/article1
and so on ... There are numerous articles.
The 404.php page appears when I'm trying to access something like sitename.com/adsdasaerera but it doesn't appear when I'm trying to access sitename.com/en/adsdasaerera, adsdasaerera not being, obviously, an existing article.
How can I achieve this?
Ideally you would have one error 404 document page. On that page you would first set a default locale of en, and then determine the users browser locale/language they are using, and proceed to the next step which is displaying the error page in that language.
Since we are talking PHP, here is an answer how to detect the locale in PHP.
Simplest way to detect client locale in PHP
From there, the next step is basically using the strings associated for that locale from an array or a file and displaying them.
On this page you could also have some links to other translations that could be viewed(hidden elements that show div on click), if the user doesn't wish to read the error in the language their browser is set to use.
Did you try giving it a full path?
ex ErrorDocument 404 /site/error/404.html
Have you tried put the line ErrorDocument 404 /404.php into the apache global config file, maybe in /etc/httpd/.
Or is there an exising .htaccess file under somesite.com/en/ folder?
how do you archieve the multi-language functionality? rewrite in apache or .htaccess? and what does your PHP code?
in addition to Branimir Đureks answer, you may analyze the $_SERVER['REQUEST_URI'] string i.e. using explode() and if the article dosn't exists in the choosen language, send header("HTTP/1.0 404 Not Found"); otherwise display the article.
If you are NOT using a CMS etc, so the Language Folders are really existing, you can use this:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(ie|en|de)/ /404.php/$1 [R=404,L]
Explanation
The three conditions check that the requested file or folder does not exist
The rule checks that the requested url starts with one of the three countries then a /, captureing the country code to Group 1
It redirects to /404.php?lang=$1, e.g. /404.php?lang=en with a 404 code
In that 404.php you just need to Use $_GET['lang'] to get the requested Langugage.
But you already said, that the multi-language functionality is archieved in .htaccess. It would be helpfull to know whats in that file.
Anyway: If all Requests get redirected to one page (e.g. index.php), somethere in that file the Content of the site gets included. That hapens either with an include or similar of a file or with a Query to the Database.
Thats the point there you need to expand your code. If that file isn't found or if there is no record in the Database, you need to include your 404 File.
Please use this code at the bottom of .htaccess. Replace Path with your path.
RewriteRule ^pagenotfound$ 404.html
ErrorDocument 404 path/pagenotfound
It doesn't redirect you to the 404.php file because you accessed the existing file. Look at this URL
sitename.com/en/adsdasaerera
"adsdasaerera" is probably only a parameter value that is rewrited by htaccess. It's not a file. I suppose you have a "en" folder and index.php in it. So when you enter url above, you access that index.php in "en" folder with parameter "adsdasaerera" and that's the reason why you don't get 404 error. You can solve it by adding little code that searches trough the database for "adsdasaerera" and if it doesn't exist, in your code manually redirect it to 404.php file.
The reason why you get 404.php on this url
sitename.com/adsdasaerera
is because you don't have file named "adsdasaerera".
Hope it will help you :)
It should be possible to define an ErrorDocument like this in htaccess file:
ErrorDocument 404 http://sitename.com/404.php
and then do a language specific redirect check for requests in 404.php file.
I have a php website with a docs folder in the root, the structure of the site is thus:
sitename.com/docs/
In the docs folder are PDF's that sometimes cause browser issues because of spaces in the names, so they are not found (mainly by IE).
What I would like to do is the following:
Whenever there is a broken URL in the docs/ directory, I would like to redirect the entire query string to a php page within the docs directory, but it must keep the name of the PDF intact.
Thus, is the URL is:
website.com/docs/this is a pdf
I want it to redirect to:
website.com/docs/index.php?pdf=this is a pdf
From there, I can grab the PDF param and fix it up and send the request to the correct file.
The reason this is not done with straight .htaccess is that I cannot find a solution that is dynamic, in other words the number of words in the PDF is variable, and could be from 1 to 20 words, separated by spaces.
I had a post up here about that at this SO post which did get one reply, however, it still does not address the variable URL length problem.
I have again tried this from the examples in this tutorial but this has not helped me at all as I cannot fathom how to do this properly.
The one thing that I think is close is the following code:
RewriteEngine on
RewriteRule ^docs/(.*) /index.php?pdf=$1 [NC]
Am I close?
First you should know that, only static codes can be written in .htaccess, and we cannot process to a dynamic code,The Following solution might help for you am not testedRewriteEngine onRewriteRule ^docs/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ /index.php?pdf=$1 [L]
Have your rule like this:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^docs/((?=[^\s]*\s).+?)/?$ /docs/index.php?pdf=$1 [NC,L,B,QSA]
This will forward all PDFs with spaces to /docs/index.php while leaving non space file names intact.
I've searched the site and got far enough where I've been successful at rewriting to a clean URL. Just need a bit more help.
I have a page with a record that I have successfully rewritten to a clean URL like so:
domain/record.php?id=1685 > to > domain/record-Gavin-Rees-1685 using the below:
.htaccess file:
RewriteRule ^record-(.*)-(.*)$ /record.php?id=$2 [L]
This in my php file:
$temp=str_replace(' ','-', $record [record_name]);
$temp=str_replace('.','', $temp);
<a href='/record-". $temp ."-".$record[id]." '>
This works perfect. The problem is.
I cannot get it to rewrite the other way so if you go directly to:
/record.php?id=1685 it still exists. i tried > RewriteRule ^record.php?id=$ /record-(.*)-(.*)$ [R,L]
This isn't possible in the .htacces rewrite rules, because you couldn't define a general rule, which knows about your software internals. If you want to do this in your .htaccess file, you have to create a rule for every single id which would result in a very large and unhandy .htaccess file.
The better way to get an redirection for direct script calls is, to do the redirect in the php file itself and set the http status codes(i.e. 301 - permanently moved).
The get the requested url, you could use $_SERVER['REQUEST_URI'] and check for /record.php at the beginning
I noticed on youtube their url does not have a file extension and querystring. I've been trying to emulate something similar but found I had to either include the file extension or a trailing slash.
members.php?agefrom=20&ageto=40&city=london (works)
members/?agefrom=20&ageto=40&city=london (works)
members?agefrom=20&ageto=40&city=london (doesnt work)
So I was just wondering how can I get the third case to work? i've tried a few things in the htaccess file.
RewriteRule ^members$ index.php?page=members&%{QUERY_STRING} [QSA,L]
I have tried the above but to no avail.
Any help would be appreciated.
The RewriteRule that you posted is correct for members.php? and for members? It should not work with members/
You must have additional RewriteRules before this one that are getting applied first and are affecting this rule.
However, here is a rule that should still work for you:
RewriteRule ^members/?$ index.php?page=members&%{QUERY_STRING} [QSA,L]
The /? is saying to match if the slash exists or if it doesn't exist.
Have you tried to remove the $ on the end?
RewriteRule ^members/?$ index.php?page=members&%{QUERY_STRING} [QSA,L]
This did work in the end, all I had to do was move it nearer the top of the htaccess file. I had the following line which I guess was being read instead.
....
RewriteCond %{REQUEST_URI} ^/members$ [OR]
....
I am changing my approach to SEO URL's because I was trying to find articles on how the googlebot actually crawls forms and how it prefers the GET method. I was using jquery to alter my action parameter to write the following URL:
/members/london/18-to-25
I dont know how much google likes jquery and whether it would scan javascript code. I am assuimg it just follows the HTML code and having done some research I have changed my form to use the GET method and so the bot can crawl my form without complaining so now my URL looks like this:
/members?location=london&agefrom=18&ageto=40
I am on the right track to assume this? or should I just stick with jquery to rewrite the action parameter for an seo friendly URL?
I am trying to redirect from one url to another using a htaccess file. I have got them all working except from one which causes an infinite redirect loop. The url I wish to redirect from is:
http://website.co.uk/author/ and i want to redirect to http://website.co.uk/author/authorname
Any ideas would be helpful
Sounds a lot like your .htaccess redirect rules are doing pattern matching on your domain name, so that when you redirect to /jamescrawford it matches against www.pragencyone.co.uk/author/ and tries to redirect again.
If you're trying to catch everything that matches http://www.pragencyone.co.uk/author/.* then you'll need to exclude http://www.pragencyone.co.uk/author/jamescrawford (and potentially any assets it uses like images, if they're in the same directory) from being matched by the pattern you're using.
You might try posting the actual rules that you're using to do the redirect, though obviously be sure not to post anything any info from your .htaccess that would compromise your server's security.
try this in your htaccess file
RewriteEngine On
DirectoryIndex index.php
AddDefaultCharset On
Options +FollowSymLinks -Indexes
RewriteRule ^author /author/jamescrawford [L]
RewriteRule ^author/ /author/jamescrawford [L]