.HTACCESS rewriting a url the basic - php

i have 2 questions regarding .htaccess,
1- if i use .htaccess to rewrite the url's on my website does it affect any of the requests or do i need to change the link for all forms for example if a action post for a form is index.php?submit do i need to change that link ? when i for example hide .php extension in .htaccess or it doesn't affect it
2-how do i basically hide all .php extensions
3- if i have the below url
www.link.com/post.php?post_id=53&post_name=the-International-day-in-roma
how do i make .htaccess rewrite it to the following
www.link.com/post/the-International-day-in-roma
does it matter if there was commas or can we remove them also ?
www.link.com/post.php?post_id=53&post_name=everyday,-or-today,-or-tomorrow

you can hide php file name in htaccess
for example if someone click this link
www.link.com/post/the-International-day-in-roma/
htaccess file contverts the url to post.php and call the the post name by $1
it represents this part "/([a-zA-Z0-9_-]+)/" the it is dynamic and it can be changed !
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^post/([a-zA-Z0-9_-]+)/$ post.php?post_id=53&post_name=$1 [L]

Related

.htaccess on 2 inputs

Right now I have the following: (this is all that is in the .htaccess file under the /page/ dir)
RewriteRule ^(.*)$ set.php?numbers=$1 [QSA,L]
This correctly pulls with the following URL:
http://domain.com/location/page/XXX/
However, when the page loads I have the following links I want to click:
http://domain.com/location/page/set.php?numbers=XXX&otherset=morenumbers
Issue:
The other link does not seem to work because the HTACCESS rules are breaking the query strings. Is there a way around this so that the links work and that htaccess does not break it?

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

How do I rewrite the URL for example.com/index.php#anchor to example.com/anchor?

I have a page (example.com) with anchors at major sections (example.com/index.php#anchor, example.com/index.php#about, example.com/index.php#contact, etc.) as well as subpages (example.com/subpage.php).
Here's my .htaccess file:
RewriteEngine On
DirectoryIndex index.php
ErrorDocument 404 http://example.com/my404page.php
RewriteRule ^anchor/?$ index.php#anchor [R,NE,L]
RewriteRule ^subpage/?$ subpage.php [NE,L]
The subpage rewrite rule works great.
For the anchor link, including the "R" (redirect) flag changes the URL to example.com/index.php#anchor, but I want the URL to be example.com/anchor.
If I remove the R flag and enter example.com/anchor into my browser, than it goes to the index page and keeps the clean URL (example.com/anchor) but doesn't scroll to the anchor.
So... Is there any way to achieve this in .htaccess, or is there a good way to achieve this with JavaScript?
It's not possible to do that with URL rewrite. Because the browser scroll down when the # is part of the URL... not on the server side.
You can't do this with .htaccess. But you can do it with the following JavaScript code:
window.location = String.prototype.replace(window.location, /index\.php#(.*)/, function(match, g1){return g1;});

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

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.

Categories