.htaccess rule gives 500 error for php file - php

I saw another stackoverflow answer to come up with this current setup.
I want requests to http://myserver.com/book/1234 to actually show the content from php file http://myserver.com/book.php?id=1234
So my .htaccess looks like this:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^id=([0-9]*)$
RewriteRule ^book/(.*)$ /book.php?id=$1 [L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]
Notice the bottom two lines allow me to use url's like http://myserver.php/login to actually pull up http://myserver.php/login.php This is working correctly.
But I get a 500 Error when I try calling http://myserver.com/book/1234 What is wrong?

Your code is instructing the server to only do the rewrite if the query string is present. As a result, the rule would only work if you requested /book/1234?id=1234.
Removing the line below will allow your rewrite to work.
RewriteCond %{QUERY_STRING} ^id=([0-9]*)$

Related

How to rewrite an url with two query inside to one folder?

I'm facing a problem to transform my actual url
http://website.com/login/profil.php?id=34&pseudo=robin
into this one :
http://website.com/myspace
I checked if the rewrite engine works well and it's ok so that's my .htaccess
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{QUERY_STRING} ^id=([0-9]+)$
RewriteCond %{QUERY_STRING} ^&pseudo=([a-zA-Z0-9]+)$
RewriteRule ^myspace\$ http://website/login/profil.php?id=$1&pseudo=$2 [R=301,L]
So my wish is to redirect the user's space on one common directory. what's the best way do do this ? am i wrong with the query ?
Thanks in advance guys !
This code doesn't work :
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteRule ^test\.html$ /profil.php?id=([0-9]+)&pseudo=([a-z]+) [L]
Try this .htaccess it will work only when get parameters are in the same order i.e.
id=32&pseudo=code
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/login/profil\.php$
RewriteCond %{QUERY_STRING} ^id=(\d+) [NC]
RewriteCond %{QUERY_STRING} &pseudo=(\w+) [NC]
RewriteRule ^login/profil\.php$ /myspace? [L,R=301]
This format works for me. You also try this:
Put this line into your .htaccess file
RewriteRule ^myspace/?$ login/profil.php?id=34&pseudo=robin
And yes, do not forget to set rewrite base in your .htaccess file:
below line will set the base url:
RewriteBase /
So, basically you need to put these both lines into your htaccess file
RewriteBase /
RewriteRule ^myspace/?$ login/profil.php?id=34&pseudo=robin
For dynamic id and name, you can use:
RewriteRule ^myspace/([0-9]+)/([A-Za-z0-9._-]+)/?$ login/profil.php?id=$1&pseudo=$2
So, into your profile.php file, you can check parameter values(id and name)by
print_r($_REQUEST);
So, example, you will have to run the url something like,
http://websitename.com/myspace/20/robin/ or
http://websitename.com/myspace/25/john/ ... and like wise.

Using RewriteRule shows apache folder navigation or 404, why?

I have wordpress installed and in the root i created a folder and put an .htaccess inside to redirect to another web page without changing the URL.
This was working fine until i move to another server (the older one was just horrible) and now it just not working.
This is code that i was using:
RewriteEngine ON
RewriteCond %{REQUEST_URI} ^/conexionalam
RewriteRule ^(.*)$ http://nuevoserver.breinguash.com/$1 [R=301,L,P]
and is no longer working, show me this:
looking for internet i have tested some other codes:
1-
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^/conexionalam(/.*|)$ nuevoserver.breinguash.com$1 [L,NC]
2-
RewriteEngine ON
RewriteRule ^/(.+) http://nuevoserver.breinguash.com/$1 [R,L]
This two show me this:
3-
RewriteEngine ON
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ http://nuevoserver.breinguash.com/$1
This one redirect me to http://nuevoserver.breinguash.com/index.html
I realized that if i took off this '[R=301,L,P]' at the end of the first code, it works but changing the URL and i don't want tat.
Thanks in advanced!
EDIT:
By the moment im using this code:
RewriteEngine ON
RewriteCond %{REQUEST_URI} ^/conexionalam
RewriteRule ^(.*)$ http://nuevoserver.breinguash.com/$1
Its redirects but change the URL = /
but for now does not affect google analytics.
if i change or add something to the code it does show me the errors from above. i really doesn't understand why it does that.
SOLUTION
Well it seem that i cant redirect between domains/subdomains without changing the URL just like that, i have to do it with proxy, so the solution was:
Enable proxy and proxy-http on apache.
Use the P flag for RewriteRule
So right now i'm using this code and it works perfectly:
RewriteEngine ON
RewriteCond %{REQUEST_URI} ^/conexionalam
RewriteRule ^(.*)$ http://nuevoserver.breinguash.com/$1 [R=301,L,P]
Give this a shot in your .htaccess file...
RewriteEngine On
RewriteBase /
RewriteRule ^conexionalam(.*)$ http://nuevoserver.breinguash.com/$1 [L,R=301]
This should redirect you to http://nuevoserver.breinguash.com when you go to /conexionalam from your main domain.

.htaccess ReWriteRule - Requested URL <rewritten filename> was not found on this server

I've been testing this in WAMP and I can't get it to work. I believe WAMP is set up properly due to the error message I'm receiving and it works no problem when I don't use the .htaccess file.
I have a filename that I'm using for testing called Feedback.php. Instead of displaying as www.mysite.com/Feedback.php. I'm trying to get it to just be www.mysite.com/Feedback.
.htaccess file
RewriteEngine on
RewriteRule ^Feedback.php/?$ Feedback [NC]
The error message that I receive is
"Not FoundThe requested URL /Feedback was not found on this server."
Are there two files required for reWriteRule to work?
I'm also trying to get this to work for my index.php to just be www.mysite.com which may be a different monster.
What should my navbar links be? Right now they are Feedback Should href perhaps be href="Feedback" once I get this working?
EDIT: I had the ReWriteRule variables backwards. Instead of
RewriteRule ^Feedback.php/?$ Feedback [NC]
I should have
RewriteRule ^Feedback/$ /Feedback.php [NC,L]
The rewrite doesn't look to have taken place though as it still reads localhost/Feedback.php
You may use this to remove .php extension from your pages as well as omitting the index part to your website:
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{THE_REQUEST} /index
RewriteRule ^index$ http://yourwebsite.com/ [R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]

Htaccess Redirect Rewrite Not working together

I want to remove the query strings from the urls using Htaccess, I used the following code for changing the urls, It did but after redirection to that url, I am getting 404 error.
and there is rewrite statement also if i use that only, then the new url works without 404 error, but the old urls doesnt get automatically redirected to new urls.
Here is the htaccess and url the I am modifying
Options FollowSymLinks
Options -MultiViews
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/profile\.php$
RewriteCond %{QUERY_STRING} ^user_id=([0-9]*)$
RewriteRule ^(.*)$ http://www.meenmipage.com/user/%1? [R=302,L,NC]
Actual Url was:
http://www.meenmipage.com/profile.php?user_id=2
and modified was:
http://www.meenmipage.com/user/2
If i remove the above code and just use the rewrite statement as like this one:
RewriteRule ^user/([^/]*)$ /profile.php?user_id=$1 [NC,L]
Then the new modified url works and the old one also works
Please tell me what to do?
I think you need to remove $ at the end of this line:
RewriteCond %{REQUEST_URI} ^/profile\.php
because if the request URI has user_id=2 then this condition will not match
Try This !
-----------
Options -MultiViews
Options +FollowSymlinks
RewriteEngine on
rewritecond %{QUERY_STRING} ^user_id=([0-9]*)
rewritecond %{http_host} ^www.meenmipage.com [nc]
RewriteRule ^([0-9]+)$ //www.www.meenmipage.com?user_id=$1 [L,QSA]

htaccess causes 500 if page doesn't exist

I am currently using this code to remove the .php extension of my files on my apache web server.
Options -Multiviews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php [L]
two questions:
1: when someone requests a page that doesn't exist, my web server is returning a 500 error instead of a 404. how can I fix this?
2: how can I force a 404 if someone requests the .php extension?
thank you in advance.
The 500 error for non-existent pages is happening because mod_rewrite is going into an infinite loop trying to rewrite your request and terminates eventually.
Rewrite the rules like this to make sure the file with the PHP extension actually exists:
Options -Multiviews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [L]
The RewriteCond %{REQUEST_FILENAME}.php -f line will cause the rewrite to take place only if "file.php" exists.
example.com/about-us.php to go to example.com/about-us.html
RewriteEngine on
RewriteRule ^(.*)\.html$ $1.php
This is not complete answer.
I try to explain how RewriteEngine works, if you don't understand that htaccess feature completely.
Apache server 'receives' some 'not real' url from your browser. Then it reads .htaccess, and by using information from that file it converts that 'not real' url to 'real' url pointing to physical file on server.
Error details are always saved to apache (httpd) error log. You have to read it, and if it not tells you anything - paste it here - we will help.
Forcing 404 error is bad idea in this case. You should fix error, not override it.

Categories