How to change query string url using .htaccess rules? - php

I have following file in root directory :
Dev (root)
update.php
zones.php
This zones.php file is showing using following url:
http://localhost/aponit/dev/zones (I hide .php extension using .htaccess rules)
In zones.php file I have a edit link to edit a form via query string. The link is bellow :
<a class="btn btn-success btn-xs" href="<?php echo SITE_URL."zones/update?z=$zone_id"?>" >Edit</a>
When I click on this link it's showing following url :
http://localhost/aponit/dev/zones/update?z=55
But in browser it's showing me error message :
Internal Server Error
Because my .htaccess rules is not define appropriate rules for that desired link.
What I want now :
I want the url should be user friendly. E.g
http://localhost/aponit/dev/zones/update/55
How can I create this link using .htaccess ?
Current .htaccess rules :
ErrorDocument 404 /not-found.php
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [NC,L]

You can have another rule to handle zones/update/55:
Options -MultiViews
ErrorDocument 404 /not-found.php
RewriteEngine on
RewriteRule ^(?:zones/)?update/(\w+)/?$ update.php?z=$1 [L,QSA,NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

Related

I am fetching friendly clear in .htaccess urls in php

I'm developing a website using PHP. I'm trying to hide any PHP extension, using .htaccess.
This is my link urls for visiting an user profile :
<a class="nav-link" href="user/<?php echo"$username"; ?>"><i class="fa fa-user ispace"></i><?php echo"$username"; ?></a>
and this is my .htaccess rules:
RewriteRule ^user/(.*)$ profile.php?user=$1 [L,QSA]
But when I click on an user profile link, my bootstrap isn't loaded.
Also, the user header link redirects to www.mysitename.com/user/home and then shows a 404 error. How to fix this?
Thanks !
To hide the .php extension:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
To hide the .html extension:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
Put the .htaccess on the main Folder.
To test just do something like:
Test go to .php file
Should work.

why .htaccess is showing me "Internal Server Error"?

In my site I removed .php extension from the url using .htaccess rules. So that I can go to any page without .php extension. e.g :
http://localhost/aponit/dev/zones (there are no .php extension)
Now,I have a link in a php page to update a form data. Link is bellow :
<a class="btn btn-success btn-xs" href="<?php echo SITE_URL."update/$zone_id"?>" >Edit</a>
This link showing following url :
http://localhost/aponit/dev/update/54
But unfortunately It's showing me error message :
Internal Server Error
I am using following .htaccess rules :
ErrorDocument 404 /not-found.php
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
RewriteRule ^update/([0-9]+) update?z=$1 [L]
Trying to:
ErrorDocument 404 /not-found.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
And post detail log file???
The .htaccess code that yuo have provided will not work correctly if the site does not run under root domain. For example
RewriteRule ^update/([0-9]+) update?z=$1 [L]
This code block tells the server that any URI starting with "update" following a number will be rewritten. but in your case, the URI always starts with "aponit/dev/". So this code will not work any time.
Solution
Set up a virtual host. You will get a lot of available tutorials online how to setup a virtual host based on your server software(XAMPP/WAMP etc)
If you just want to remove .php from url, use the following code in .htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

How to hide folder names and file extension in URL?

I have following requirements which i believe can be accomplished using .htaccess file.
Requirements-
Hide Folder names and file extension in URL Eg. www.example.com/subfolder/subfolder1/file.php should become www.example.com/file
Restrict Folder browsing - I want to restrict folder browsing capability when somebody fires an URL eg. www.example.com/subfolder Conventionally by firing this URL user will be able to browse through the contents of subfolder. By firing such URL or any URL containing domain example.com eg. www.example.com/folderNotExist then server should redirect to index page.
I am able to restrict folder browsing but redirection to index page and hiding of folder and file extension is not working.
You can have these rules in your root .htaccess:
ErrorDocument 404 /
DirectoryIndex index.php
RewriteEngine On
RewriteCond %{THE_REQUEST} /subfolder/subfolder1/ [NC]
RewriteRule ^ / [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/subfolder/subfolder1/$1.php -f
RewriteRule ^(.+?)/?$ subfolder/subfolder1/$1.php [L]
You should try something like:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*?)$ /subfolder/subfolder1/$1.php
ErrorDocument 404 /index.php
you can try this
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www.mysample.com
RewriteRule ^subdir/(.*)$ http://www.mysample.com/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f

htaccess RewriteRule to shorten the URLs

I would like to find a RewriteRule that does this :
mysite.net/jqMAS/ or mysite.net/jqMAS => mysite.net/index.php?id=jqMAS
I used such a .htaccess file :
RewriteEngine On
RewriteRule ^(.*) index.php?id=$1
But unfortunately, it doesn't work (maybe mysite.net/index.php is itself redirected to mysite.net/index.php?index.php, etc. ?) : calling mysite.net/jqMAS produces a 500 Internal Server Error.
What RewriteRule should we use to do such URL shortening ?
Here is what the index.php page (I didn't mention the headers) looks like :
<body>
Bonjour <?php echo $_GET['id']; ?>
</body>
Try the following your .htaccess file, as it is the setup successfully used on my own website.
# Enable the rewriting engine.
RewriteEngine On
# Change requests for a shorter URL
# Requires one or more characters to follow the domain name to be redirected
RewriteRule ^([A-Za-z0-9-]+)/?$ index.php?id=$1 [L]
There are 2 htaccess files:
Keep your htaccess file within application folder as:
Deny from all.
and paste following code to your htaccess file outside the application folder:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
It is working perfect for me.
You need RewriteCond to stop rewriting for real files and directories:
RewriteEngine On
# if request is not for a directory
RewriteCond %{REQUEST_FILENAME} !-d
# if request is not for a file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php?id=$1 [L,QSA]

htaccess two commands in the same file

Hi i have my htaccess with this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.htm [L]
ErrorDocument 404 /404.htm
if i erase the first 4 lines, the other piece of code (which redirects de 404 page when there's a page not found) does not work anymore.
Any ideas?
I also want that when it redirects to 404.htm, it does not show on the url box, this url
http://www.mypage.com/404.htm
i want to show this
http://www.mypage.com/fail/search-by-the-user
for example.
Try following .htaccess
RewriteBase /
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ /$1.html
ErrorDocument 404 /404.html
NOTE: if you are working at localhost then please mention proper RewriteBase

Categories