Mod_rewrite not working on subdirectory on localhost - php

I have this folder system:
localhost/websites/2018/mywebsite
And there I have this file:
products.php
so the full route I type in my browser to access it is:
localhost/websites/2018/mywebsite/products.php
Now, I'm trying to use mod_rewrite to get this:
products.php?cat1=a&cat2=b&cat3=c
into this:
shop/a/b/c
So the full url would be:
localhost/websites/2018/mywebsite/shop/a/b/c
I tried putting the .htaccess in here:
localhost/websites/2018/mywebsite
with this code:
RewriteEngine On
RewriteRule ^shop/([^/]*)/([^/]*)/([^/]*)$ /products.php?cat1=$1&cat2=$2&cat3=$3 [L]
But when I go to
localhost/websites/2018/mywebsite/shop/
I get a 404. I think it has to do with the directory tree because when I upload it to the website (root directory) it works fine.
Edit: I tried using
RewriteBase localhost/websites/2018/mywebsite
And some variations but it didn't work. It threw 500 server error.

You would need to use %{QUERY_STRING} for this to work. Try something like the following:
RewriteEngine On
RewriteCond %{QUERY_STRING} cat1=(.+)&cat2=(.+)&cat3=(.+)$ [NC]
RewriteRule ^ /shop/%1/%2/%3/? [R=301,L,NE]
This grabs each of your variables and then rewrites /shop/a/b/c onto the end of your URL. The use of ? is there to stop the original query from appearing on the end of the URL.
Make sure you clear your cache before testing this.

Related

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

How do I get mod-rewrite to access the proper directory?

I'm trying to use mod-rewrite to eliminate urls like {root}/index.php?action=about&page=faq. I want {root}/about/faq to redirect to the php url. My .htaccess file looks like
DirectoryIndex index.php
RewriteEngine On
RewriteRule ^(index|about|calendar|news|contact)$ index.php?action=$1 [L]
RewriteRule ^(index|about)/(.+)$ index.php?action=$1&page=$2 [L]
When redirecting to index.php?action=$1, this works fine. However, it doesn't work completely when the second rule is used. The html content appears fine, but the stylesheet and images are missing. The page seems to think that it's in a subdirectory. So {root}/about/faq looks for images in the folder about/images, instead of in the images folder found at the root. The PHP script says that the working directory is still the root directory, however. How do I make the pages think that they are in the root directory? Thanks!
RewriteRule ^(index|about|calendar|news|contact)/(.*)/$ /index.php?action=$1&page=$2
Would result in /about/faq/ rewriting to index.php?action=about&page=faq
/calendar/month/ would result in index.php?action=calendar&page=month
Also unless you want to rewrite imagesfiles I would add:
RewriteCond %{REQUEST_URI} !(\.gif|\.jpg|\.png)$ [NC]
This will stop image requests being rewritten.

Failed to load resource: CodeIgniter (works fine locally)

I've just transferred a project of mine to the web and I have a file called "circlecrop.php" that's located in the root directory of where all the CodeIgniter files are. This php script basicall just makes any image with circlecrop.php?path=img cropped to a circle, very simple and it works fine locally.
This works perfectly fine locally.
I'm getting the following error:
Failed to load resource: the server responded with a status of 403 (Forbidden)
I can't access circlecrop.php directly even though I've got it set to 777, I haven't anything set within my routes in CodeIgniter I'm not sure it's needed?
Complete htaccess file
RewriteEngine on
RewriteBase /
RewriteCond $1 !^(index\.php|circlecrop\.php|images|js|profile_pictures|fonts|stylesheets|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
I've done some searching and can't seem to find anything, my only thought is that I need a way to directly access circlecrop.php and it will work. Any help is greatly appreciated!
Edit: I've updated the url that has the errors, maybe that's more helpful.
You need to include if your website placed on root directory
RewriteBase /
If it place in test folder for example use this.
RewriteBase /test/
simply use site_url() on every link and action instead of base_url().
This worked for me:
I replace .htaccess content by the following:
RewriteEngine on
RewriteCond $1 !^(index.php|application/themes|application/theme_path|images|assets|robots.txt)
RewriteRule ^(.*)$ index.php/$1 [L]
Update your base_url according to your live host in config.php
$config['base_url'] = 'SET YOUR SITE URL HERE';
config.php is located in /application/config

URL Rewriting in Apache Not Working For Me

I am using WAMP and has created a website in a 'http://localhost/snap2/html' folder. I am trying to execute following Rewrite rule but this is not working for me.
Server is giving me an error below:
The requested URL /snap2/html/browsed.html was not found on this server.
My .htaccess file is located in html folder and its structure is as below:
`RewriteEngine On
RewriteRule ^decision/([0-9]+)$ /snap2/html/decision.php?PanelID=$1`
Website is in a structure like 'www/snap2/html
Infact I am trying to rewrite following url
http://localhost/snap2/html/decision.php?PanelID=20
in to
http://localhost/snap2/html/decision/20
Also Options +FollowSymLinks gives me an error 500 therefore, I have commented it.
Any help would be pretty much appreciated.
Try this:
RewriteEngine On
RewriteRule RewriteRule ^snap2/html/decision/([0-9]+)$ /snap2/html/decision.php?PanelID=$1
But I guess this is not what you want. But maybe you will see where you wrong.
Symbol ^ in regular expression means that following string should be at the beginning of URL. so you have to include entire path, see htaccess RewriteEngine for more examples
Your write RewriteRule twice. Try
RewriteRule decision/([0-9]+)$ /snap2/html/decision.php?PanelID=$1 [L, QSA]
Or
RewriteRule snap2/html/decision/([0-9]+)$ /snap2/html/decision.php?PanelID=$1 [L, QSA]

Categories