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'/>.
Related
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
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="/" />
I have used .htaccess in the past to rewrite my URLs.
I used things like this:
RewriteRule event-([0-9]+)-([^.]+)\.html$ /event.php?id=$1 [L]
BUT really prefers directory style:
RewriteRule ^events/([a-z]+) events.php?cat=$1 [NC]
My question is that it has always bothered me in the HTML with the directory style to add this kind of code to link images, includes(); and other links when the directory gets long when with the first style I don't have to change anything.
../../../images.jpg
../../../../../script.php
Is there an other way to code this because it's really bad like thisnd gets on my nerves each time.
Assuming that you have the folder structure like :
css [folder]
images [folder]
event.php
events.php
index.php
etc.
You could use in your html : base and at that point you can just link your css and images like normal.
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'] ?>">
I have problem with this rule:
RewriteRule ^([^/]+)/(\d+)$ more.php?books=$1&tags=$2 [L]
After this rule everything works but page is displayed incorrectly. Like missing css.
Thanks in advance
This might happen, when you have a relative URL to your CSS files. If you access the URL /book-name/2 and have a link to css/style.css, for example, the browser will resolve this to /book-name/css/style.css.
Change your CSS links to absolute URLs, like /css/style.css, and you should be fine.