Rewritten url not being changed in url bar - php

I made a page which loads content dynamically depending on the alias that was sent. This works fine, but the url stays uglified.
Example:
https://website.nl/aanbieding.php?alias=this-is-the-alias
But I want:
https://website.nl/aanbieding/this-is-the-alias.html
htaccess rule:
RewriteRule ^aanbieding/(.*).html /aanbieding.php?alias=$1 [L]
I got the exact same htacess rule for another page, and that one works like I want.
This one:
RewriteRule ^diensten/(.*).html dienst.php?alias=$1 [L]
When I click from the page itself to another page (same file on the server but different loaded content) the url turns into what I want. Only when I open it from an email template the url stays ugly.
I link from the mail template like this:
Bekijk aanbieding
Why is the url changing into my example?

Related

how to access same directory file in .htaccess

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

Creating SEO friendly urls using htaccess

I am trying to rewrite the urls of a site, i should mention that the way index.php works now is getting the p (page) parameter and including the appropriate file.
So requesting a page is like this:
www.domain.com/index.php?p=home
www.domain.com/index.php?p=search
www.domain.com/index.php?p=profile
www.domain.com/index.php?p=contact
I found how to create a rewrite rule for this:
RewriteRule ^([A-Za-z0-9-]+)/?$ index.php?p=$1
so now www.domain.com/home would give me the home page
But i also need to have a friendly url for this
www.domain.com/index.php?p=profile&id=20
to
www.domain.com/profile/20/profile-friendly-name
or better
www.domain.com/profile/profile-friendly-name
*The profile-friendly-name refers to a company name
Requirements:
To have friendly urls for all pages e.g. /home, /contact
To have a particular friendly url for the profile page with the
profile name in the url
My questions:
How can i add a profile-friendly-name to the url with the existing url format (index.php?p=profile&id=20)?
Can i only have a unique name there (without the id), like my last
example?
If i manage to do that, will i have to change ALL the existing urls
within the site (links, urls to images, to files) to the friendly
format?
I noticed that after applying the first RewriteRule some css
stylesheets and js are not included. What is wrong?
RewriteRule ^profile/([0-9]+)/([A-Za-z0-9-]+)/?$ index.php?p=profile&id=$1
Should work for :
www.domain.com/index.php?p=profile&id=20
to
www.domain.com/profile/20/profile-friendly-name
Ex 1 :
https://example.com/books.php?bookName=php using htaccess url will be written like this https://example.com/php
You can do this using code given below in .htaccess file:
RewriteEngine On
RewriteRule ^([^/\.]+)/?$ books.php?bookName=$1
Ex 2 :
https://example.com/books.php?bookName=php&&chapter=array using htaccess url will be written like
https://example.com/php/array
You can get by using code in .htaccess file given below:
RewriteEngine On
RewriteRule ^([^/\.]+)/([^/\.]+)?$ books.php?bookName=$1&chapter=$2

How To: URL Rewrite & HTML Link Within Same Page?

I want a user to click on link of www.domain.com/how-it-works
My URL Rewrite is:
RewriteRule ^how-it-works/?$ index.php?action=about&link=howItWorks [NC]
index.php GETS "action" and "link" parameters, and then this is where I get lost...
I am currently using the following:
require( TEMPLATE_PATH . "/about.php" );
This loads up the about.php template file, which is exactly what I want, but then where do I put the "same page link" of: #HIW? #HIW is a link inside the about.php template file.
The #HIW in your url is called a fragment identifier in HTML.
Supposing you are using apache, you can go with (note the NE and R flags at the end):
RewriteRule ^how-it-works/?$ index.php?action=about#HIV [NC,NE,R]
(I took of &link=howItWorks because I suppose #HIV replaces it, if not you can put it back)
http://httpd.apache.org/docs/2.2/rewrite/flags.html#flag_ne
As far as I understood your URL Rewrite already carries that link information. In this case the HIW you mentioned above as the final loading url is index.php?action=about&link=howItWorks.
Why don't you $_GET['link'] inside the about.php and write the output from that result?
I mean, if you want to give the answer from the about.php file, you can get the link information with $_GET[] and give your desired result information from there.
Expand your rewrite to allow inner links:
RewriteRule ^how-it-works/?(#.*)?$ index.php?action=about&link=howItWorks [NC]
Please not I don't know if this reg ex works I just typed it out quickly. Please try a few if its not working properly.
It should 'just work' :)

.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'] ?>">

after .htaccess url rewrite, cannot perform logoff in some of the url rewrited page

Recently, I was doing .htaccess url rewrite, make all my php url into html, in some page, the logout button wont work properly. for example, in page ‘quotedetails/Q9999.html’ (rewrited from ‘quotedetails.php?quoteID=Q9999′), when I click logout button in this page, it wont do the trick, but when i use the old php url of this page, it works again, other rewrited pages like index.html (index.php), search.html(search.php), all works perfectly.
I use firebug to debug, after I click the logout button, it stays in the same page without redirect me to the index.html, but I saw the the ‘logoff’ params has been passed through, but just dont let me logout and redirect to index page. I’ve changed all the relavent file path to absolute path, still no luck…..help please.
I’ve also noticed from firebug, that page cannot get the redirect ‘location’ as I tried in other pages, their response headers come with ‘location: index.html’, but in that no-workin-page, there is no such line called ‘location: index.html’ in its response headers.
Here is my .htaccess file, no-workin-pages are related to the first four ReweiteRules
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^reps/all,all.html$ rep.php?repID=all&repName=all
RewriteRule ^reps/([A-Z]+),([A-Za-z\sA-Za-z]+).html$ rep.php?repID=$1&repName=$2
RewriteRule ^reps/([A-Za-z]+),([A-Za-z\sA-Za-z]+),([0-9]+).html$ rep.php?repID=$1repName=$2&page=$3
RewriteRule ^quotedetails/(Q[0-9]+).html$ quotedetails.php?quoteID=$1
RewriteRule ^index.html$ index.php
RewriteRule ^addquote.html$ addquote.php
RewriteRule ^search.html$ search.php
RewriteRule ^viewall.html$ viewall.php
RewriteRule ^howto.html$ howto.php
all the CSS will be lost, how to fix this issue?
Use absolute path for all the CSS files and images
I click log out button, its not working
You have to do at least initial debug. Nobody here knows, what's going on when you press a button. Go figure.
You don't have to use absolute paths... most people just forget about one of the most important html-tags. write this into your -section of your html-output:
<base href="http://mysite.com" />
Now all your css-files and images should be loaded correctly.

Categories