htaccess rewrite condition to a php file in WP - php

I have this htaccess file in the 'public_html' folder:
RewriteEngine On
RewriteRule ^asset/(.*)$ asset.php?code=$1 [NC]
Essentially, the rewrite condition should work as follows. When a user clicks on a link ../asset/XXX, the asset.php file generates a new webpage containing details of the XXX asset.
The lookup is not working as I think the htaccess file cannot find the 'asset.php' file.
The asset.php file is located in
'public_html/wp-content/themes/theme1/asset.php'
How can I modify the htaccess file to lookup this file?

Have you tried:
RewriteRule ^asset/(.*)$ wp-content/themes/theme1/asset.php?code=$1 [NC]

The rewrite is from your directory where the htaccess file is located. So add the path to the destination file.
RewriteEngine On
RewriteRule ^asset/(.*)$ wp-content/themes/theme1/asset.php?code=$1 [NC]
Perhaps you can set the RewriteBase to /. Sometimes that fixes some problems.

Related

How can I maintain a website where a folder and file have the same name but also hide the extension of that file?

I am trying to improve the structure of my urls by using .htaccess. I have a file named foo.php and a folder named /foo. I want to be able to access example.com/foo and show example.com/foo.php. I also want to be able to access example.com/foo/bar and show example.com/foo/bar.php. Lastly I want to reditrect from example.com/foo/ to example.com/foo. Does anyone know how to do this?
This is my code:
RewriteEngine On
DirectorySlash Off
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^.]+)$ $1.php [NC,L]
I am able to remove the .php extension by I am not able to redirect from the folder to the file. I also tried the solutions below but none of them resulted in the desired behaviour.
htaccess - Rewrite files that have a directory with the same name
.htaccess, rewriting of filename with same name as directory
.htaccess, proper rewriting of directory and file with same name
I found a workaround solution to this problem. In order to remove the extension from file names, I used the following code in my .htaccess file:
RewriteEngine On
DirectorySlash Off
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
The above code allows me to go to example.com/foo and show example.com/foo.php. In order to redirect from example.com/foo/ to example.com/foo, I created an index.php file in the /foo directory. I added the following PHP code to the file
<?php
$page = str_replace('/index.php', '', $_SERVER['PHP_SELF']);
header("Location: $page");
?>
This redirects from the directory to the file. Hope this helps.

Making server redirect to another folder with .htaccess

Can i make a htaccess file that redirect from the root of my project to another folder.
So if i usually write 192.168.1.39/public to get to my library where i store the files that i want the users to see.
I would like to only write 192.168.1.39 instead.
Can you do that with a .htaccess file?
In the htaccess file in root, add this rule :
RewriteEngine on
RewriteRule ^$ /folder [NC,L]
^$ matches the hompage and rewrites it to the folder.

How to URL redirect/rewrite using the .htaccess file

How to write .htaccess rewriterule for the below URL.
I am new to htaccess.
http://velloredentists.com/search.php?s=VASAN+DENTAL+CLINIC&c=Bagayam )
to
http://velloredentists.com/search/VASAN+DENTAL+CLINIC/Bagayam
Please help me to solve this.
This worked on my apache server :
RewriteEngine On
#Redirect "/search.php?s=foo&c=bar to
#/search/foo/bar
RewriteCond %{THE_REQUEST} /search.php\?s=([^&]+)&c=([^\s]+) [NC]
#Rewrite "/search/foo/bar" to
#"/search.php?s=foo&c=bar"
RewriteRule ^ /search/%1/%2? [NC,R,L]
RewriteRule ^search/([^/]+)/([^/]+)/?$ /search.php?s=$1&c=$2 [QSA,L,NC]
1 - First you need to check if the file .htaccess exists in the root of your website.
2 - If the file .htaccess exists, download it from your site, add the lines below to the end of the file, and upload the modified file back to your site. Please make a copy of the original file .htaccess. So if anything goes wrong, you may restore the original file.
3 - If the file .htaccess does not exist, create a new file .htaccess and add the lines below to it. Then upload it to the root of your website. You may delete it if anything goes wrong.
Add the following lines to .htaccess:
RewriteEngine On
RewriteRule ^search/([^/]*)/([^/]*)\.html$ /search.php?s=$1&c=$2 [L]

rewrite url using htacess

I have test project folder called "prj". In that, I have one file called test.php.I want something like
http://localhost/prj/test.php?t=123
should be rewritten like
http://localhost/prj/test.php
any help will be appreciated
Create a .htaccess file with:
RewriteEngine on
RewriteRule ^prj\/test.php$ prj/test.php?t=123 [NC]
Edit
If your htaccess file is located in the prj folder:
RewriteEngine on
RewriteRule ^test.php$ test.php?t=123 [NC]

How to use rewriterule to redirect to a php file in the same folder?

I would like to take requests for /somefolder/style.css and handle them with /somefolder/program.php
So, I put the following in my .htaccess file:
rewriteengine on
rewriterule ^style.css$ program.php?css=1 [R=302,L]
The result is that instead of redirecting to /somefolder/program.php, the server tries to redirect to:
/var/www/html/somefolder/program.php?css=1
How can I get rid of the /var/www/html/ in the redirect? I thought that since I just entered program.php in the .htaccess that it would default to the same folder.
Since this is a generic script that I will use in many places, I would like to avoid using rewritebase to specify the folder I'm in -- the .htaccess has to work in any folder without being modified.
Leave the R flag away and you will get an internal redirect:
RewriteRule ^style\.css$ program.php?css=1 [L]
Otherwise specify the full URL path you want to redirect to externally:
RewriteRule ^style\.css$ /program.php?css=1 [R=302,L]
Or for any arbitrary folder:
RewriteCond %{REQUEST_URI} (.*)/style\.css$
RewriteRule ^style\.css$ %1/program.php?css=1 [R=302,L]
I think the problem is that you are missing a ReWrite base statement.
Also the I would put the .htaccess file in the root directory of your site. That way you don't have to copy an .htacess file into every new directory you create. What if you want to change the name of your php file? You'd have to change every .htaccess file.
This is how I would redirect www.mydomain.com/orange/style.css to www.mydomain.com/orange/program.php?css=1 using generic rules:
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)/style\.css$ $1/program.php?css=1 [L]

Categories