URL Rewriting with .HTACCESS not working in php - php

My URL looks like this:
http://127.0.0.1/website/comments.php?topic_id=16
I want to make it look like this:
http://127.0.0.1/website/comments/my-news-article/16.php
I have applied this
RewriteEngine on
RewriteRule ^comments/([a-zA-Z0-9_-]+)/([0-9]+)\.php$ comments.php?id=$2
I have also turned on Rewrite_module in Apache.
But it's not working.

Always consider the entire URL path. Yours includes /website, so:
RewriteEngine on
RewriteBase /website
RewriteRule ^comments/([a-zA-Z0-9_-]+)/([0-9]+)\.php$ comments.php?id=$2

RewriteBase is used for per-directory directives. Try
RewriteEngine on
RewriteRule ^/website/comments/([a-zA-Z0-9_-]+)/([0-9]+)\.php$ /website/comments.php?topic_id=$2
Works for me. Note that I also changed the query string parameter at the end of the line to "topic_id". Maybe this is another cause of error. If it still fails, check that the configuration is in the right virtual host config.

Related

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]

Very simple .htaccess mod_rewrite configuration gives 404

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

php file renaming virtually wamp environment htaccess

Does PHP supports htaccess URL rewriting in Windows - WAMP Environment?
How can I use it?
Below is one example:
My existing file name and URL is like below:
http://localhost/um/api_user_registration.php?FIRSTNAME=first_name&LASTNAME=last_name&USERNAME=user_name&EMAIL=abc#abc.com
Can I use it like below:
http://localhost/um/UserRegistration.mac?FIRSTNAME=first_name&LASTNAME=last_name&USERNAME=user_name&EMAIL=abc#abc.com
In short, I want to access file as UserRegistration.mac instead of api_user_registration.php
Please suggest a way of doing.
URL rewriting is done using mod_rewrite, an apache module. Make sure that it's loaded (probably in httpd.conf) and then add this to the htaccess file in your /um/ folder:
RewriteEngine On
RewriteBase /um/
RewriteRule ^UserRegistration\.mac$ api_user_registration.php [L]
You can try like this:
RewriteEngine on
Options +FollowSymlinks
RewriteBase /yourproject
RewriteRule ^UserRegistration.mac/([^/]+)/(.*) api_user_registration.php?id=$1 [PT]
more info: http://goo.gl/EBzz6t

mode rewrite function in .htaccess file

Hom many times can I use mode_rewrite function and rule in .htaccess file?
like:
RewriteEngine On
RewriteBase /public_html
RewriteRule ^([a-zA-Z0-9\-]+)$ /game.php?game_name=$1
You can use as many as you like, as long as you add them on newlines. Make sure your rules are not getting into a conflict though.
Some information about parameters you can use:
http://httpd.apache.org/docs/current/rewrite/flags.html
You can test your .htaccess with this website:
http://htaccess.madewithlove.be/

Why doesn't this mod_rewrite rule work?

When I try to use this rule:
RewriteEngine On
RewriteRule ^articleID/([^/]*)$ /viewArticle.php?articleID=$1 [L]
It sends me to a 404 page.
I am not well-versed in how to use an htaccess, so I most likely did something wrong. I generated it with a tool at generateit.net
I'm trying to access http://majornoob.com/devel/testdesign/articleID/5 which would equal http://majornoob.com/devel/testdesign/viewArticle.php?articleID=5
Try to add a RewriteBase:
RewriteBase /
Also make sure that your server accept htaccess files. Does the Server configure contains a AllowOverride all statment in your Directory Directive.
With the path, it should work:
RewriteRule articleID/([^/]*)$ /devel/testdesign/viewArticle.php?articleID=$1 [L]

Categories