z0mbieKale
z0mbieKale

Reputation: 1028

How to rewrite url for certain pages in htaccess?

I am trying to show different url and redirect users to specific url with .htcaccess when they click on a blog post but to no avail.

Lets say the url is: http://localhost/mySite/article.php?article_title=test-title

then I would like to show it as http://localhost/mySite/article/test-title

This is my current htcaccess file:

#turn on url rewriting
RewriteEngine on

#remove the need for .php extention
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

#rewrite rule for blog
RewriteRule article/([A-Za-z0-9-]+) /mySite/article.php?article_title=$1

But for some reason it is not redirecting/showing the correct url. I am not getting any errors.


EDIT

Trying to ask my question again and explain it better. Let's say the url is

http://localhost/www.example.com/admin/editUser.php?user_id=126

and I would like to rewrite the url like this:

http://localhost/www.example.com/admin/user/126

then how can I achieve this. I tried using this website to check the modified url but it does not work. Seems like it does not work with any of the accepted answers here in stack at all.

This is my htaccess file atm. It is in the root of www.example.com

#turn on url rewriting
Options +FollowSymlinks
RewriteEngine on

RewriteRule  ^user/([0-9]+)/?$ /editUser.php?user_id=$1 [NC,L] # Handle user edit requests

Apache Module mod_rewrite is enabled. Also added an alias. Still no changes in the url. If I try something really basic like this:

# redirect to .php-less link if requested directly
RewriteCond %{THE_REQUEST} ^[A-Z]+\s.+\.php\sHTTP/.+
RewriteRule ^(.+)\.php $1 [R=301,L]

it works fine.

Why is the users redirect not working? What am I doing wrong.

Upvotes: 1

Views: 39

Answers (1)

Abhishek Gurjar
Abhishek Gurjar

Reputation: 7476

Try it like this for your rule for article url in mysite directory.

RewriteRule ^article/([A-Za-z0-9-]+)$ article.php?article_title=$1 [QSA,NC,L]

you need to mention start ^ and end $ of string.

Upvotes: 1

Related Questions