I'm trying to get a wordpress page to "run" another page based on the url. So if I have a main page like:
/extensions
That will run the regular extensions page. But if I have urls like:
/extensions/test
/extensions/test/again
/extensions/text/again/etc
They will all just run the extension (no "s") page:
/extension
The extension page will parse the url and do it's thing at that point. I tried setting up a redirect rule in the .htaccess file like so:
RewriteCond %{REQUEST_URI} ^/extensions/.*
RewriteRule . /extension [L]
But I can't seem to get it going. I'm assuming wordpress is always parsing via the index.php file, which is some how trumping my little rewrite possibly. Is there anyway to set this up on wordpress?
Found the answer here.
Note that this should go into the functions.php file and you will have to hit save in the permalinks settings EVERY time you make a change to the function to see the effects.
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'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.
i tryed to search everywhere for this problem but i didnt found nothing.
I want to make make a url seo friendly so i used this code:
RewriteEngine on
RewriteRule ^Homepage index.php [NC,L]
Then i want to redirect to it so i tryed to write this code:
RewriteRule ^index.php$ http://localhost/siti/socialmark/Homepage [R=301,L]
The error it's a loop of redirections, can someone help me?
SORRY FOR MY BAD ENGLISH!
The rewrite rules don't just make the URL string look different, it actually directs the user to the file at the end of the path even if you don't see it in the address bar. If Homepage is a directory containing index.php, even if that php file name doesn't appear in the URL, then it's causing a loop because it's directing you to a directory with an index.php.
The rule is executed every time that page loads. So, you're redirecting to a page which runs the redirect script, so it runs the rule to redirect again, and that causes the loop. What you want to do is create a condition that says "Don't run this code if the requested page is http://localhost/siti/socialmark/Homepage"
Something like this (you may have to adjust it)
RewriteBase /
RewriteCond %{REQUEST_URI} !=/siti/socialmark/Homepage
RewriteRule ^Homepage index.php [NC,L]
For more details, see the caveats and example here:
http://httpd.apache.org/docs/2.2/rewrite/flags.html#flag_l
In my php page I have a hyperlink like:
Remove User";
The delete_user_page.php via GET takes the name of the user and executes the script. I want a URL rewrite mechanism inorder to hide the passed variable. So I wrote:
.htaaccess
RewriteEngine On #Turn on the re-writing engine
RewriteRule ^delete/?$ delete_user_page.php?name=$1 [NC,L] # Handle requests for "Delete users page"
When loading this onto the server and refreshing the results nothing has changed/showed so I realized I am doing something wrong.
Where does htcaccess have to be located relative to delete_user_page.php?
Looking at "delete_user_page.php?name=$1", is $1 correct or should it be different?
If there is something else wrong in my script please tell me.
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