URL Rewriting in Apache Not Working For Me - php

I am using WAMP and has created a website in a 'http://localhost/snap2/html' folder. I am trying to execute following Rewrite rule but this is not working for me.
Server is giving me an error below:
The requested URL /snap2/html/browsed.html was not found on this server.
My .htaccess file is located in html folder and its structure is as below:
`RewriteEngine On
RewriteRule ^decision/([0-9]+)$ /snap2/html/decision.php?PanelID=$1`
Website is in a structure like 'www/snap2/html
Infact I am trying to rewrite following url
http://localhost/snap2/html/decision.php?PanelID=20
in to
http://localhost/snap2/html/decision/20
Also Options +FollowSymLinks gives me an error 500 therefore, I have commented it.
Any help would be pretty much appreciated.

Try this:
RewriteEngine On
RewriteRule RewriteRule ^snap2/html/decision/([0-9]+)$ /snap2/html/decision.php?PanelID=$1
But I guess this is not what you want. But maybe you will see where you wrong.

Symbol ^ in regular expression means that following string should be at the beginning of URL. so you have to include entire path, see htaccess RewriteEngine for more examples

Your write RewriteRule twice. Try
RewriteRule decision/([0-9]+)$ /snap2/html/decision.php?PanelID=$1 [L, QSA]
Or
RewriteRule snap2/html/decision/([0-9]+)$ /snap2/html/decision.php?PanelID=$1 [L, QSA]

Related

Mod_rewrite not working on subdirectory on localhost

I have this folder system:
localhost/websites/2018/mywebsite
And there I have this file:
products.php
so the full route I type in my browser to access it is:
localhost/websites/2018/mywebsite/products.php
Now, I'm trying to use mod_rewrite to get this:
products.php?cat1=a&cat2=b&cat3=c
into this:
shop/a/b/c
So the full url would be:
localhost/websites/2018/mywebsite/shop/a/b/c
I tried putting the .htaccess in here:
localhost/websites/2018/mywebsite
with this code:
RewriteEngine On
RewriteRule ^shop/([^/]*)/([^/]*)/([^/]*)$ /products.php?cat1=$1&cat2=$2&cat3=$3 [L]
But when I go to
localhost/websites/2018/mywebsite/shop/
I get a 404. I think it has to do with the directory tree because when I upload it to the website (root directory) it works fine.
Edit: I tried using
RewriteBase localhost/websites/2018/mywebsite
And some variations but it didn't work. It threw 500 server error.
You would need to use %{QUERY_STRING} for this to work. Try something like the following:
RewriteEngine On
RewriteCond %{QUERY_STRING} cat1=(.+)&cat2=(.+)&cat3=(.+)$ [NC]
RewriteRule ^ /shop/%1/%2/%3/? [R=301,L,NE]
This grabs each of your variables and then rewrites /shop/a/b/c onto the end of your URL. The use of ? is there to stop the original query from appearing on the end of the URL.
Make sure you clear your cache before testing this.

Rewriting url in apache is not working

I am using the following code in .htaccess direct editor in ipage server for changing my website's page url from
for example:
http://foodinger.in/viewRestaurant.php?raipur=Barbecue-Country&id=3006
to
http://foodinger.in/viewRestaurant/raipur/Barbecue-Country
but it is not working
RewriteEngine on
RewriteRule viewRestaurant/raipur/(.*)/ viewRestaurant.php?raipur=$1&id=$2
RewriteRule viewRestaurant/raipur/(.*) viewRestaurant.php?raipur=$1&id=$2
Am i doing anything wrong, Please anyone suggest me the right way if i am wrong ?
Thanks in advance
You specify only one group for substitution (.*) that will replace $1 in target url.
Try:
RewriteEngine on
RewriteRule "^/viewRestaurant/raipur/(.*)/(.*)/?" "/viewRestaurant.php?raipur=$1&id=$2"
And query with Id: http://foodinger.in/viewRestaurant/raipur/Barbecue-Country/3006

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.

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]

Apache Rewrite rule confusion

I'm trying to convert a simple url (below) in to a blog-style url, but not quite sure how to do it, all of my other rules are working fine, but I can't seem to figure this one out.
URL I want to convert: http://www.website.com/myblog.php?id=1&title=My+blog+title
URL I want it to create: http://www.website.com/1/my-blog-title
What should the rule be?
Any assistance appreciated :)
Try this
RewriteEngine on
RewriteBase /
RewriteRule ([0-9]+)/([^.]+) myblog.php?id=$1&title=$2
Try this in your .htaccess file:
RewriteEngine on
RewriteRule ^(\d+)/([^/]+)$ myblog.php?id=$1&title=$2
But here the hyphens are not replaced by plus signs.
in your .htaccess file,
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)$ /myblog.php?id=$1 [L]
You don't (well shouldn't) need to pass the blog title to the blog file, only the ID. Hope this works

Categories