Very simple .htaccess mod_rewrite configuration gives 404 - php

Ok, I'm starting to think that the problem is me but... ¿What's wrong here?
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteRule testpage\.html http://www.google.com [R] #It works
RewriteRule ^mineral/([0-9]+)/?$ ver.php?id=$1 [NC,L] #It doesn't
I have a folder called "WebX" and this .htaccess inside it along with other files from the web.
mod_rewrite is working ok according to phpinfo and everything is running on localhost.
The most courious thing is that if I type localhost/WebX/testpage.html it redirects to Google
but unfortunately for me if I type localhost/WebX/mineral/1 it gives me 404. ¿What happens?

The problem you are having is caused by RewriteBase /. Your .htaccess is in a subdirectory /WebX/, but you are telling mod_rewrite to rewrite rules as if the current directory was /. It tries to load a file ver.php in your www-root, which doesn't exist. If you have verbose error messages with what file it tried to load, you'll notice it says that it tried to load /ver.php and not /WebX/ver.php as you might expect.
The rest you are doing, most notably using a relative url instead of a / prefixed absolute url, seems correct. The correct .htaccess to use would be:
Options +FollowSymLinks
RewriteEngine on
RewriteBase /WebX/
RewriteRule testpage\.html http://www.google.com [R] #It works
RewriteRule ^mineral/([0-9]+)/?$ ver.php?id=$1 [NC,L] #It now should work

Related

.htaccess simple rewrite rule not working in subdirectory

I'm working on a subdirectory, and want to rewrite ugly file-urls into prettier URLs.
Like:
domain.com/myfolder/myreport.php --> domain.com/myfolder/MyReport/
Here's what I have:
RewriteEngine On
RewriteRule ^test/?$ index.php [NC,L]
Taken from here
However, it's not working. I've tested every combination of regex I could imagine, nothing worked.
Are there any other potential problems? Is it possible that all rules are ignored/overwritten by a rule in the root directory or something?
The .htaccess IS being used, if I add "test", I get a 500 Error.
Can you just try the following,
RewriteEngine on
RewriteBase /myfolder/
RewriteRule ^(.*)$ /myfolder/index.php?/$1 [L]

mod_rewrite into subdirectory file from folder

This could be probably duplicate due to unable to find that original even after checked the similar questions list of stackoverflow. Actually if user type myipaddress/gt then it should redirect to myipaddress/gt/gt.htm
I tried various mod_rewrites but still gets the 'gt' folder contents listed in browser. I gave the last try with below mod_rewrite.
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteRule ^gt$ gt/gt.htm [NC,L]
But still the browser shows the contents of the 'gt' folder. What am I doing wrong?
You can use this rule in /gt/.htaccess:
Options +FollowSymlinks
RewriteEngine On
RewriteBase /gt/
RewriteRule ^/?$ gt.php [L]
Due to the fact that Apache sees gt as a directory, it treats it as such. However, that doesn't stop all rewriting for that particular path.
This is the basic process:
You navigate to /gt.
Apache sees that as an existing directory, and thus redirects to /gt/.
Your rule is ignored, because it doesn't contain the trailing slash.
This is why you still see the directory listing.
As such, you should change your rule to this:
RewriteRule ^gt/$ gt/gt.php [NC,L]
Alternatively, you can make use of DirectorySlash off and make the trailing slash optional, like so:
Options +FollowSymlinks
DirectorySlash off
RewriteEngine On
RewriteBase /
RewriteRule ^gt/?$ gt/gt.php [NC,L]
Doing either of the above allows you to have multiple rules of a similar nature in a single .htaccess file.

RewriteRule .htaccess multiple params

I'm trying to rewrite my url, but I'm getting an error "500 Internal Server Error"
I've never done this before, and I've read some tutorials but I can't say I got any smarter from it.
The .htaccess file is in the root directory (/var/www)
Options +FollowSymLinks
RewriteEngine On
RewriteBase /sub_domain/directory_to_my_files/
RewriteRule ^([0-9.]+)-([0-9.]+)/(.*)/$ /index.php?pos=$1:$2&text=$3
The current link goes like this:
http://sub_domain.my_domain.com/directory_to_my_files/index.php?pos=(float|integer-only):(float|integer-only)&text=(any-text)
But I'm trying to do this:
http://sub_domain.my_domain.com/directory_to_my_files/(float|integer-only):(float|integer-only)/(any-text)/
Sorry if the links is a bit hard to read.
This should be placed in /directory_to_my_files/.htaccess
RewriteEngine On
RewriteBase /directory_to_my_files/
RewriteRule ^(-?[0-9.]+)[:-]([0-9.]+)/([^/]+)/?$ index.php?pos=$1:$2&text=$3 [L,QSA,NE]
I've tested this locally:
( I did not need the rewrite base )
RewriteEngine on
RewriteRule ^([0-9.]+)-([0-9.]+)/(.*) /index.php?pos=$1:$2&text=$3 [R,L]
And it has redirected me
from
http://loc.domain/555-666/misc-text
to
http://loc.domain/index.php?pos=555:666&text=misc-text
I reckon this was what you wanted?
About the [R,L] at the end:
the R is telling apache to actually redirect, not just rewrite the url - this helps with testing.
the L is saying this is the last rule
Edit: could it be that the rewriteBase is causing you problems? Try it without and see if it works - at least you can nail where the problem is, then.

Cant redirect my URL with htaccess

I am using WAMP and my url is localhost/hotel/hotels.php?id=21
Using rewrite rule as follow,
Options +FollowSymLinks
RewriteEngine on
RewriteRule hotels/id/(.*)/ hotels.php?id=$1
RewriteRule hotels/id/(.*) hotels.php?id=$1
But nothing happens..
My mod_rewrite is on and also changes done in httpd.conf file.
Please give me a suggestion to handle the issue?
You must use flags for your RewriteRule. You can change it as follow
RewriteEngine on
RewriteRule ^hotels/id/([\d]+)/?$ hotels.php?id=$1 [NC,L]
You can call your pages from
localhost/hotel/hotels/id/12
If your .htaccess file is located in localhost/hotel .

Pass a clean parameter through URL

I'm using localhost, and in my index.php page I have this code:
<? echo 'LANG IS '.$_GET['lang']; ?>
When I type localhost on the URL it only shows LANG IS, obviously, but if I type localhost/en I see a 404 Not Found message. I have to type localhost?lang=en to show my index.php code. I want to type localhost/en instead of localhost?lang=en and get the same result.
I'm using Apache2 and I have mod_rewrite enabled. I also have a .htaccess file with this code (I have changed and tested it a lot of times):
RewriteEngine on
RewriteRule ^/([a-zA-Z0-9]+|)/?$ index.php?lang=$1 [L,QSA]
I have been reading about .htaccess and clean urls for days but I couldn't make this work. Any ideas? Thank you so much in advance.
Most likely your .htaccess isn't even enabled. Verify it first
To check if your .htaccess is enabled try putting same random/garbage text on top of your .htaccess and see if it generates 500 (internal server) error or not?
It it is not enabled then then you will need AllowOverride All line in <Directory "/var/www/>` section.
Once it is enabled following rule should work for you:
RewriteEngine on
RewriteRule ^(\w+)/?$ index.php?lang=$1 [L,QSA]
Try getting rid of the leading slash in the pattern:
RewriteEngine on
RewriteRule ^([a-zA-Z0-9]+|)/?$ index.php?lang=$1 [L,QSA]
URI's that are sent through rules in the htaccess files have the leading slash stripped off so the pattern needs to omit it.
The problem is probably in the regular expression.
Try with this one:
RewriteEngine on
RewriteRule ^/([a-zA-Z0-9]+)/ /index.php?lang=$1 [L,QSA]

Categories