remove php file name with extention and replace with value - .htaccess

I am starting to use htaccess in my project and learned basics such as removing extensions . I am trying to remove a file name in php and replace it with a value being passed in url for eg
example.com/eg/check_folder.php?folder=folder-ck
to
example.com/eg/folder=folder-ck
but i am not able achieve this i made the following code. Please guide me with where i am wrong .
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/http://example.com/eg/(\d+)/?$ /http://example.com/eg/check_folder.php?folder=$1 [NC,L,QSA]

First verify whether your .htaccess is enabled or not, by putting same garbage text on top of your .htaccess and see if it generates 500 (internal server) error or not.
Once you make sure it is enabled make sure mod_rewrite is also enabled.
Finally use this code in /eg/.htaccess:
RewriteEngine on
RewriteBase /eg/
RewriteRule ^(folder=.+)$ check_folder.php?$1 [L,QSA,NC]

I got this working by using the following code inside my eg folder
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ check_folder.php?folder=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ check_folder.php?folder=$1

Related

.htaccess rule help in a plex non-windows hosting environment

I'm having trouble writing a rule for .htaccess which will redirect HTML to PHP. I'm using 302 until I get it to work right - then I'll change to a 301. I found several postings that describe this, but I am having problems - possibly because I'm running a hosting package, and each client is in a virtual/subfolder (sorry for poor description of hosting environment).
The rule I am using is
...
RewriteEngine on
RewriteRule ^(.*).html$ $1.php [R=302]
...
When I try to go to https://dmcelebratealife.com/index.html I get a 404 message saying:
https://www.dmcelebratealife.com/var/www/vhosts/dmcelebratealife.com/public_html/index.php
I added the following and it seemed to work for me.
RewriteEngine on
RewriteRule ^(.*)\.html$ /$1.php [R=302]
This works for me and is domain-independent.
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(?!.+\.\w{2,4})(.+)$ $1.php [L]
RewriteCond %{DOCUMENT_ROOT}/$1.html -f
RewriteRule ^(?!.+\.\w{2,4})(.+)$ $1.html [L]
The first two redirect to the php file if it exists. If it doesn't then the second two will try for an HTML file.
Note it doesn`t use a 302 as you don't want to tell the user what you are doing. This does an internal redirect.
This means that if the user types in
https://www.example.com/test
it will remain in the URL box, but the following file will be executed
https://www.example.com/test.php

htaccess replace the parameter in the url

I have a URL like this :
https://example.com/coins/1&order_by=today and several categories such as:
https://example.com/coins/1&order_by=new
https://example.com/coins/1&order_by=trending
You can also change the page so it will be 2 instead of 1 etc.. like : https://example.com/coins/2&order_by=today
I would like to change these to have https://example.com/today?page=1 etc instead of this above
I have read many responses on this subject and I searched the web, but unfortunately no solution works.
I tried this but it does not works:
RewriteEngine on
RewriteBase /
RewriteCond %{QUERY_STRING} ^(.*&)?order_by=new(&.*)?$
RewriteRule ^(.*)$ $1?new/%1/%2 [L]
Try following htaccess Rules, please make sure to place these Rules at top of your htaccess file. Make sure to place your htaccess file inside root folder along with coins folder(not inside coins folder).
Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteBase /
RewriteCond %{THE_REQUEST} \s/coins/(\d+)&order_by=(\S+)\s [NC]
RewriteRule ^ /%2?page=%1 [R=301,L]

Got stuck in a very basic .htaccess rewrite

<h1>' . $name. '</h1>
The above is the reference which points to my profile1.php file. This file is called index.php . It currently displays the urls as this:
http://www.domain.com/interact/profile1.php?id=36
I have tried implementing the .htaccess file to rewrite the url. I tried many combinations and most of them gave a 500 error and some did not rewrite the url.
This is the .htaccess file which I use, it does not make the url to change.
I want the url to look like http://www.domain.com/interact/profile/36
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ profile1.php/$1 [QSA,L]
</IfModule>
I know this is a very basic question but I seem to stuck in it and have read basic tutorials but am not able to implement it properly.
The files index.php ,profile1.php and .htaccess are in folder named interact.
Tell me any changes required in php or .htaccess files.
It currently displays the urls as this: http://www.domain.com/interact/profile1.php?id=36
...
I want the url to look like http://www.domain.com/interact/profile/36
Step 1:
Change your content to have links like this:
<h1>' . $name. '</h1>
This way, when you click on a link the URL that will appear in the URL address bar is will look like: http://www.domain.com/interact/profile/36
Step 2:
Then you need to use these rules to internally change it back:
RewriteEngine On
RewriteBase /interact/
RewriteRule ^profile/([0-9]*) profile1.php?id=$1 [L,QSA]
In order to point any external links, like google index bots to the new URLs, you'll need to add these as well:
RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /interact/profile1\.php\?id=([0-9]*)
RewriteRule ^ http://www.domain.com/interact/profile/%2 [L,R=301]
try this
RewriteRule ^interact/profile1.php/(.*)$ interact/profile1.php?id=$1 [L]
i don't know what make QSA modifiers does so i remove it. If you know what, use it)

htaccess RewriteRule redirecting for URL's without trailing slashes

I have this snippet in my .htaccess file to defend against anyone trying to get into the app directory.
RewriteCond %{REQUEST_URI} ^/app.*$
RewriteRule ^(.*[^/])/?$ index.php?r=$1 [L,QSA]
And although it works when I go to http://domain/app/, if I make a request to http://domain/app, it redirects to http://domain/app/?r=app.
Does anyone know what needs to be changed to stop such redirection?
Try the DirectorySlash directive that can be use globally or per directory.
http://httpd.apache.org/docs/2.2/mod/mod_dir.html#directoryslash
try using
[L]
instead of
[L,QSA]

Change Displayed URL Structure using mod_rewrite NOT Working

I need to change the structure of the displayed client-side URL. I'm not too skilled using regex and coding for the .htaccess file. Basically, I have a structure that looks something like:
http://www.example.com/catalog/index.php?cat=lt&sec=lt1-1&id=nmlt10.
I would like this to be displayed in the address bar as:
http://www.example.com/catalog/lt/lt1-1/nmlt10.
This is what I came up with, but it has had no effect:
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)/([^/]*)\$ /catalog/index.php?cat=$1&sec=$2&id=$3 [L]
I tested and removed any other rules in the .htaccess file to ensure nothing was being overwritten. I'm on a shared hosting apache server, and know that mod_rewrite is enabled, because I use it to rewrite non-www to www urls. I don't receive and 500 error messages, I just do not notice any change at all. I'm not sure where I'm going wrong here, so hopefully someone can point me in the right direction.
Finally found a solution that worked:
RewriteEngine On
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ index.php?cat=$1&sec=$2&id=$3 [QSA,L]
Appreciate LazyOne's response to get me on the right track; however, when using:
RewriteEngine On
RewriteRule ^([^/]+)/([^/]+)/([^/]+)$ index.php?cat=$1&sec=$2&id=$3 [QSA,L]
I wasn't able to following links that were already placed on the site, it treated different directories as the variables, for example, when browsing to an image or file, say:
folder/folder/image.png
It would grab "folder" - "folder" - and "image" as the variables. I can see why that was happening, if anyone has a different solution or an explanation, please let me know, I'm always willing to learn.
Since your .htaccess is in website root folder, then you should use thus rule:
RewriteEngine On
RewriteBase /
RewriteRule ^catalog/([^/]+)/([^/]+)/([^/]+)$ /catalog/index.php?cat=$1&sec=$2&id=$3 [QSA,L]
If you place it in .htaccess in /catalog/ folder, then you can remove catalog from it:
RewriteEngine On
RewriteRule ^([^/]+)/([^/]+)/([^/]+)$ index.php?cat=$1&sec=$2&id=$3 [QSA,L]
I have tested rule before posting -- works fine for me.
This rule (same as above) will check if URL is a file or folder and will only rewrite if it is not:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/([^/]+)$ index.php?cat=$1&sec=$2&id=$3 [QSA,L]

Resources