writing mod_rewrite to load a particular file - php

I am trying to load a page (/user/profile.php) whenever the URL is sitename.com/u/[user_nicename]
How to achieve this by mod rewrite ?
I have tried the following but it is not redirecting properly,(shows 404 err)
here is my htaccess content,
RewriteEngine On
RewriteRule ^u/([^/.]+)/?$ /user/profile.php [L]
(I have created this .htaccess file in 'u' directory so that when a request like example.com/u/[something] is made, it will look for [something] file, when that is not found it tries to find 'u' directory where I have written this redirect)
Kindly help, all I am trying is to load a particular file ie., /user/profile.php whenever a request is like sitename.com/u/[user_nicename]
Thanks!

try this:
RewriteRule ^/u/(.*) /user/profile.php [L]

Since, you have this .htaccess inside the sub-directory u is implicit and not required in the rule.
RewriteEngine On
RewriteRule ^([^/]+)/?$ /user/profile.php [L]

Related

How can I handle in PHP a subfolder's index.php as main index.php

I read this question: How to set an index page in a subfolder.
I want to make like this, I have these folders and files:
index.php
items/
items/index.php
and I want, when I search for "items/general", then items/index.php handles it, not index.php. I tried to make a .htacces file in /items/.htacces, with this:
RewriteEngine On
RewriteRule ^/?$ items/index.php [L]
BUT when I search for "items/general", it gives a 404 error, like this:
Not Found
The requested URL was not found on this server.
(and my items/index.php includes a code to send html to all request)
Why doesn't works it?
you can use something like that :
RewriteRule items/ items/index.php [L]
if you want a genaric rule /any-folder/any-file.php to /any-folder/index.php
RewriteRule (\w+)/ $1/index.php [L]

Htaccess rewrite rule php link not working Wordpress

I've made some search about .htaccess Rewrite function, but I couldn't find any example with an html reference.
I'm trying to redirect from my example.com/products/category/ link to another page.
Here's the link what I'd like the .htaccess file to redirect:
<a href="example.com/products/category/?category=bathroom">
I'd like to achieve this style:
example.com/products/category/bathroom/
Here's my .htaccess ReWriteRule:
RewriteRule ^products/([A-Za-z0-9-]+)/?$ /products/category.php/?category=$1 [L] # Process category
Note that I'm using two different php pages for both. page-category.php and category.php
I've places it after
RewriteEngine On
RewriteBase /
but still it doesn't work.. The only time something happened when I tried some combinations, it threw back an internal server error. Is there a problem with my RegEx or am I not doing the linking right ?
EDIT: I'm using wordpress, I forgot to mention that, sorry.
You need to use Redirect 301
RedirectMatch 301 ^example.com/products/category/?category= /example/home/page-category.php
Or you can use mod_rewrite instead:
RewriteEngine On
RewriteRule ^example/products/category/?category= /example/home/page-category.php [L,R=301]
same you can do for another file.
as i understood from your question you need something like this in category directory .htaccess file
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) category.php?category=$1 [QSA]
Now anything that is not a directory or a file inside category directory will be rewriten like /category/bathroom will be rewriten to (without redirection) /category.php?category=bathroom

Htaccess Redirect for API

I want to redirect every .htaccess call to a particular file.
I want to redirect all Get/Put/Post/Delete calls to a folder to a specific index.php file.
I have following directory structure:
\var\www\html\app
inside app i have a file index.php
Now i want any calls like
GET http(s)://myWeb.com/app
POST http(s)://myWeb.com/app
GET http(s)://myWeb.com/app/helloworld
to redirect to
\var\www\html\app\index.php
Here is my failed attempt:
RewriteEngine On
RewriteBase /app/
RewriteRule ^ app/index.php [QSA,L]
#RewriteRule ^ index.php [QSA,L] //Tried this one as well
This .htaccess is inside
/var/www/html
I also placed a similar htaccess inside app folder but it doesnt works too.
Should i use 2 htaccess files ? If one what should be the format for it ?
Also, is there a tool to check htaccess regex ? something like www.regexr.com
Did you try this?
RewriteRule . app/index.php [R,L]
I think QSL is needed only if you want to pass the URL queries

htaccess redirecting to index issue

I got a error/issues with my .htaccess codes. The htaccess file is located in a sub-directory "domain.com/subdir/.htaccess". The thing is that when I access "domain.com/subdir/" is ok but when I try "domain.com/subdir/test/run.php" the browser wont proceed to the said url but rather stick to "domain.com/subdir". And if the file/dir does not exists its just go to the "domain.com/subdir" page and the url on the address bar still "doamin.com/subdir". Sorry for the poor english.. My Code below on my htaccess
RewriteCond %{REQUEST_URI} ^(.*)/index.php$
RewriteOptions Inherit
RewriteRule .* index.php [L]
Can anyone show me the right code. Aim is to proceed to any url even is a 404 or not not always sticking to the "domain.com/subdir"
Add this to your .htaccess file
RewriteCond $1 !^(index\.php|test|any_other_directory)

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