i have something like
RewriteRule ^home$ ?get=main
inside my .htaccess to redirect any url calls from mysite.com/index?get=main to mysite.com/home
but when i use any quotation marks like ' and " after that url it dosen't behave correctly and dosen't access the file in question, for example if i type
mysite.com/home' or mysite.com/home" (notice the quotations) it redirects off to another page and dosen't access the file at all which should be mysite.com/index?get=main and when i use mysite.com/index?get=main' or mysite.com/index?get=main" it accesses those files then.
i want it to either access the mysite.com/index?get=main file from mysite.com/index?get=main" or give a 404 error page, right now it's doing neither, what's going wrong here?
The rewrite rule is looking for an explicit value of home to match to the ?get=main.
Try
RewriteRule ^home ?get=main
Related
I want to create a pretty rule in php using .htaccess.
something like localhost/search/name instead of using the normal PHP way like search?q=name. But am stuck in implementing it because all my page are now returning error 404 and I don't know why.
Here is my .htaccess file:
RewriteEngine on
RewriteRule ^localhost/?$ search.php?name=$1 [QSA]`
Here is my search.php file:
<?php
echo $_GET['name'];
?>
So when I visit something like localhost/search/myname it returns error 404
What am I doing wrong?
You're going to want to first also include the /search/ as part of the rewrite rule and then i would also suggest adding in the 'L' flag after QSA so the rule is the last rule processed when called.
You then want to capture the name from the url and pass this through as a get param to the view.
I've also ommitted localhost from the URL as you don't need to target this.
try this:
RewriteRule ^search/(.*)$ search.php?name=$1 [QSA,L]
If someone tries to access this page:
www.myhomepage.com/m/40921
I want the URL (above) to remain the same but the content should be from this page:
www.myhomepage.com/msg?t=40921
If it must be exactly like you said, then use this
RewriteRule ^m/([0-9]+)\/?$ msg?t=$1
But I think you made a typing error. Shouldn't it be msg.php? If so, use this:
RewriteRule ^m/([0-9]+)\/?$ msg.php?t=$1
And in your php file, you can get the t with $_GET['t'].
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 URL that is like the following:
http://www.example.com/client/project/subdirectory/value/
I would like like a simple way to be able to change/redirect the URL to the following:
http://www.example.com/client/project/#/subdirectory/value/
Once the redirect is complete, the hash needs to be accessible via JavaScript. I'm okay with a full refresh/redirect, just ideally that I write this once and don't have to change it again.
In other words, when the site goes live, the URLs will be structured differently, so that:
http://www.example.com/subdirectory/value/
Will change to:
http://www.example.com/#/subdirectory/value/
Edit:
I have tried using this:
RewriteRule ^profile/?$ #/profile/ [ NC,L]
Which doesn't seem to do anything
Also tried this:
RewriteRule ^profile/?$ /#/profile/ [NC,L]
Which takes me to the root directory
Also tried this:
RewriteRule ^profile/?$ #/profile/ [R,NC,L]
Which adds the whole root path to the server, followed by /%23/profile/
If you have a URL like the following:
http://localhost/tests/redir/subdirectory/value/
And you want to get it redirected to:
http://localhost/tests/redir/#/subdirectory/value/
Place a .htaccess file into the directory of tests/redir with the following:
RewriteEngine On
RewriteBase /tests/redir
RewriteRule ^(.*/)$ #/$1 [R,L,NE]
And you will get the wanted redirect. The R flag plays together with the RewriteBase directive. Also the NE flag is necessary so that you can put # literally into the redirect URI.
The hash is already used by page anchor tags. You might need to replace it with an entity or pick a better character.
ok assume i have php page
has this name name.php?get= and has get varible named get
ok
how i can make it appear like that name?get=
If you are using apache, mod_rewrite is one way to go. There is a whole bunch of mod_rewrite tricks here.
I'd seriously reconsider before using (or overusing) mod_rewrite.
In almost all of my projects I use a simple mod rewrite in the .htaccess:
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^.*$ ./
AddHandler php5-script .php
This tells the server to forward all pages to / (index.php) unless a file otherwise exists.
In the root directory I have a folder called "views" with all of the pages that I use. E.g. the file used for /home would actually be /views/home.php. However, in the index.php I have a script that parses the user's url, checks for the file, and includes that.
$page = substr($_SERVER['REQUEST_URI'], 1);
if(!$page) :
header("Location: /home");
if(file_exists("views/$page.php")) :
include "views/$page.php";
else :
include "views/$page.php";
endif;
This creates a variable called $page that stores the value of everything in the URL after the domain name. I use a substr() function on the Request URI to get rid of the trailing forward slash (/) on the URL.
If the variable is empty, for example if the user is simply at http://example.com or http://example.com/ then it forwards them to /home, where the script then checks for the home.php file inside of the views folder. If that file exists, it includes it, and displays it to the user.
Else, the script will simply include the 404 page telling the user that the file doesn't exist.
Hopefully this helps you, and if you need any further explanation I'd be happy to help!
I think you're wanting to re-write the URL client-side, which would include mod_rewrite.
In the route of your website, create a file called .htaccess and place the following code in it:
RewriteEngine on
RewriteRule ^name?get=(.*) /name.php?get=$1
Now when you type http://www.example.com/name?get=something, it will actually map to http://www.example.com/name.php?get=something transparently for you.
As far as i could understand your question, you can not strip the file extension because otherwise it will not run. In other words, you can not change:
name.php?get=
into
name?get=
But if you mean to create links with query string values that you can put them in hyperlinks in this way:
Click here !!
If you're looking to create links using a variable '$get', then you can create the link like this:
<a href="name.php?get=$get>Link</a>
Or if you want to get the value of the query string variable, you can use this:
$get = $_GET['get']