Change url from profile.php?user=myUsername to profile/myUsername with .htaccess - php

I am using xamp to host my webpage on my PC and I want to change this url from
http://localhost/loginsystem/profile.php?user=myUsername
to
http://localhost/loginsystem/profile/myUsername
I have tried the following and doesn't work and in return gives server error 500.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^/profile/([a-zA-Z0-9]+) /profile.php?user=$1 [NC, L]
and
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^/profile/(.*)?$ /profile.php?user=$1 [NC, L]
Any help would be great still pretty new. Thanks in advance!

This should do the trick
RewriteRule ^profile/([^/]+)/?$ profile.php?user=$1 [L]
Also make sure that your .htaccess file is in the loginsystem folder. Otherwise you will need to put this in your rewrite rule
RewriteRule ^loginsystem/profile/([^/]+)/?$ profile.php?user=$1 [L]

Replace .htaccess code with:
Options +FollowSymLinks
# Turn mod_rewrite on
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^profile/([a-zA-Z0-9]+)/$ profile.php?user=$1 [L]
Now you can get user query with GET request.

Related

Issue with .htaccess for friendly urls

This is my currently my .htaccess file
Options -MultiViews
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [NC]
RewriteRule ^(.*)/edit/(\d+)?$ $1/edit.php?tag=$2 [NC]
As you can see it's set to hide the .php extension, but this then seems to break the edit rule. If I comment out
RewriteRule ^(.*)$ $1.php [NC]
The edit rule works fine, but I need both and cannot seem to get it to work, anyone see what the problem is and how to sort it.
[Edit]
I have a link like this and when all rules are active this is what doesn't work.
http://www.domainname.com/researcher/lists/edit/
and I get an 500 Internal Server Error.
Try rules as in your site root .htaccess:
Options -MultiViews
RewriteEngine on
RewriteRule ^(.+)/edit(?:/(\d+))?/?$ $1/edit.php?tag=$2 [NC,L,QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

How to change the URL in htaccess

I have created all the file in my folder
Now when I am accessing the product_2.php I am getting the URL
http://localhost/Test/product_2
Now I have to display the URL like
http://localhost/Test/product/product_2
I added RewriteRule ^product/product_2 product_2.php in the .htaccess but it's not working.
.htaccess file
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteRule ^product/product_2 product_2.php
Would you help me out in this issue?
Try adding the following line to your .htaccess file.
Redirect /Test/product/product_2 http://localhost/Test/product_2
You should put it before and with a little change
RewriteRule ^product/product_2 product_2 [QSA,NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
Anyway, this is not the best practice I would recommend.. you should redirect all of your calls to index.php and handle them there, something like that:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
then in index.php
$requested_url = $_GET['url'];
and from here you can decide what to do, you can include the wanted file or even do something more clever.

Rewrite url to site.php as site

Here is my files
index.php
blog.php
about.php
I am accessing it as
localhost/index.php or localhost
localhost/blog.php
localhost/about.php
How can i rewrite .htaccess so that i can access it by
localhost/
localhost/blog
localhost/about
Update
My image are not displaying once i applied the below provided .htaccess, can any give fix this ?
Simple try this in .htaccess file :
RewriteRule ^([a-zA-Z0-9_-]+)$ $1.html
Options -MultiViews # optional Depends on Server
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
EDIT:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
In your .htaccess file
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

Changing "index.php" Within The Address

I am developing a website in which i want to change my page home.php into page home
I tried this .htaccess code
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php page=/$1 [L,QSA]
Please help me.
Just this:
RewriteEngine On
RewriteRule ^(.*)$ $1.php [L,QSA]
Try this:
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
# Replace html with your file extension, eg: php, htm, asp
This will work on local, but when you release your project don't forget to check if rewrite is enabled for the current server.

htaccess remove .php and keep query string

Here is my .htaccess file right now.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php [NC,L,QSA]
This works in the fact that it makes my pages accessible when not using the .php extension.
Old = domain.com/test.php
New = domain.com/test
The bad thing is that when I send get data with the following link the data is not passed. I thought the QSA option did that, whats the deal?
domain.com/test?id=1
Matching the entire query string and appending it to your new URL using a back-reference should work.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^(.*)$ $1.php?%1 [NC,L,QSA]
Here's a more simple solution. We use it on our team project. It will work not only in the root, but in any directory of your website.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
If you are passing id then you have to use this code
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^test/(.*)$ /test.php?id=$1
now you can access
domain.com/test?id=1
from
domain.com/test/1

Categories