how to access same directory file in .htaccess - php

I have facing a problem is when the url is http://localhost/admin/add_department/11 then I clicked a button which under same folder and will go to general_setting.php but the url will became http://localhost/admin/add_department/general_setting and still at add_department page ,
I want it became http://localhost/admin/general_setting,
this is my current .htaccess file content
RewriteEngine On
RewriteBase /
RewriteRule ^(admin)/([\w-]+)/([\w-]+)/?$ test/application/$1/$2.php?action=edit&department_id=$3 [L,QSA,NC]
RewriteRule ^(admin)/([\w-]+) test/application/$1/$2.php [L,QSA,NC]
http://localhost/admin/general_setting is work under my current .htaccess file,how to redirect http://localhost/admin/add_department/11 to http://localhost/admin/general_setting
thanks~

Only the server knows your folder structure. So if you want to go from http://localhost/admin/add_department/11 to http://localhost/admin/general_setting you should make your link point to ../general_setting instead of just 'general_setting'.
Update
A second option is to make all URLs relative to the domain. So if you want to go to http://localhost/admin/add_department/11. You make the link /admin/add_department/11 and if you want to go to http://localhost/admin/general_setting you make the link /admin/general_setting. The slash on the beginning means that it has to look from the domain you're on.
You might have to set a base tag depending on your situation.
<base href="http://localhost">
Documentation of base tag

Related

Redirecting pages from under subpage to under root path

Some pages on my site appear under example.com/subpage/. but I want to show them under example.com/. How should I change the following redirects?
For example I want to do
from: https://example.com/subpage/XXXXXX
to: https://example.com/XXXXXX
My RewriteRule
RewriteRule ^subpage/([0-9a-zA-Z-_]+) another-page.php?page_slug=$1 [L,QSA]
When I remove "subpage" from above code, the paths of the website's css and js files also change.

Problems with Apache mod_rewrite

I decided to use mod_rewrite to make my URLs look better. I created very simple rules:
RewriteEngine on
RewriteBase /
RewriteRule ^(profile|contact|help|events|account|logout)/?$ index.php?p=$1 [NC]
RewriteRule ^home/?$ index.php [NC]
RewriteRule ^PlaceOrder/([0-9]+)/?$ index.php?p=mos&gc=$1 [NC]
It almost works well but it doesn't. So for example, [NC] is supposed to make it case-insensitive. However, for some reason, when I type for example localhost/Help in the browser, for some strange reason, it redirects to home page.
Another issue is with the last rule. If I type in localhost/PlaceOrder/1 it works as expected and opens index?p=mos&gc=1. But after that if I click on, for example, Account button, the browser uses this URL: localhost/PlaceOrder/account. Which is wrong. It should only use localhost/account. Why is it adding that sub-directory in there? It is only happening after the last rule is used. Prior to using the last rule, all links work well.
I'm going to guess that the localhost/Help isn't because of the rules and because of something in your index.php script. There's nothing those rules do that could possibly rewrite Help to the home page.
The second issue is a matter of a relative vs absolute URL issue. You're probably using relative links in your pages and because you've changed your relative URL base (which would be /PlaceOrder/ all relative links will have that prepended to it. You need to either change all your links to absolute URLs (they'd start with a /) or add this to your page's header:
<base href="/" />

.htaccess referencing images from subdirectories

I'll try to make this brief. This is my first attempt using HTACCESS to create "user-friendly URLs". I've set up my .htaccess file so that when a user clicks a link that appears to be in a subfolder, it loads a file within the root.
ex:
RewriteEngine On
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^current-communities/(.*)$ $1 [L]
So if I click a link like "/current-communities/my-community it will load the file "my-community.php". No problem there.
However, now I'm trying to make another rule that when I click a "floor-plans" link, the floor-plans.php file will load data from a database based on its querystring:
RewriteRule ^floor-plans/([^/]+)-([^/]+).php floor-plans.php?communityname=$1&modelname=$2 [NC]
So if a user clicks on a link like "/floor-plans/my-community-myhouse", it will load the "floor-plans.php" file, with "my-community" as the first querystring variable, and "myhouse" as the second.
The issue I'm having here is that the "floor-plans.php" file is not showing the pictures. It is looking into a folder called "images" for graphics, but because "images" is actually within the root directory, I only get a very empty page, but for some reason this is not the issue with the virtual directory of "current-communities".
Can you help?
Hello try using the next code in the head of your document, basically what it does is to set path for all scripts and css files to the actual root of the php document and not what the address in the browser is set up to your using.
so what it actually does is once set in the head before importing anything else php will automatically set the path if you give on css/style.css, php will set it before it goes back to the browser to http://www.myweb.com/css/style.css
it works fine for my wbesites hope it helps you.
<BASE href="http://<?php echo $_SERVER['SERVER_NAME']."/".$_SERVER['PHP_SELF'] ?>">

What is the correct form of href attribute in <a> tag while working with Apache mod_rewrite?

I want to make my URL clean. Below is what I want to do:
Dirty URL: www.site.com/index.php?page=products
Clean URL: www.site.com/products/
For this, I write these codes in .htaccess file:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^([^/]*)/$ /index.php?page=$1
But whenever I click on the links in my project, I redirect to localhost page. Links are in this form:
Products
I think it may be related to href value.
What's the correct form for href value? href="product",href="/product/",etc. ?
There is no correct form for your href attribute, each form acts in different ways:
Products
The above would go relatively from the page you are currently on, so might be domain.com/products but might be domain.com/shop/products
You are better using a link direct from the root by using a / at the start, for example:
Products
would go to domain.com/products (note the lack of a / at the end of the url, which will make it not work with your current htaccess rule)
To make both /products and /products/ go to the correct place, change your htaccess rule to:
RewriteRule ^([^/]*)/?$ /index.php?page=$1
The ? will make the character preceding it optional, so the / in this case.

mod_rewriter change my url

I have some problems with mod_rewrited at .httacess.
We have created a website, the website links have this sctructure.
www.myweb.com/page.php?title=this-is-the-title&filmID=454122
AND
www.myweb.com/video.php?title=this-is-the-title&filmID=2567971&player=veevr
AND
www.myweb.com/gallery/index.php?galeriID=11
is possible to change this structure to:
www.myweb.com/page/this-is-the-title/454122.html
www.myweb.com/video/this-is-the-title/454122/veevr.html
www.myweb.com/gallery/index/11.html
Any help will be really great
Thank you for reading the post!
Best Regards
JoinOG
In your .htaccess file in your web root folder put the following code.
RewriteEngine On
RewriteRule ^page/([^/.]+)/([0-9]+).html/?$ page.php?title=$1&filmID=$2 [L]
RewriteRule ^video/([^/.]+)/([0-9]+)/([^/.]+).html/?$ video.php?title=$1&filmID=$2&player=$3 [L]
RewriteRule ^gallery/index/([0-9]+).html/?$ gallery/index.php?galeriID=$1 [L]
What this does is mask the url /page/something/12345.html to page.php?title=something&filmID=12345. It masks it, so when you go to the first URL it still looks like the first URL in the address bar but is really at the second URL. Simple tutorial on how this works is here: http://corz.org/serv/tricks/htaccess2.php
The server will think you are in the folder /page/something/ so if your CSS, images and hyperlinks are locally relative links they will not work, e.g. it will look in /page/something/yourimage.png for an image linked to like this <img src='yourimage.png'/>. To get it to work as you'd like it to, you'll need to put a forward slash before all your links to make it relative to your website's root folder like this <img src='/yourimage.png'/>.

Categories