htaccess: RewriteRule for URL rewriting - php

I have a link that looks like this
http://www.example.com/artistmusic?address=JKanyomozi
I would like to change this URL to
http://www.example.com/JKanyomozi/music
I've only been able to do this by adding music as a parameter instead of adding it to the new URL as a prefix.
In short, i've turned this URL ...
http://www.example.com/artistmusic?address=JKanyomozi&req=music
... using this rule ...
RewriteRule ^([^/]*)/([^/]*)$ /artistmusic?address=$1&req=$2 [L]
... into ...
http://www.example.com/JKanyomozi/music
But the problem with this is that for other pages like the one for the videos, this rule redirects to the same page http://www.example.com/JKanyomozi/music instead of http://www.example.com/JKanyomozi/videos
The videos page's original URL is something like this ...
http://www.example.com/artistvideos?address=JKanyomozi
Which rule can I use to change the URL above to
http://www.example.com/JKanyomozi/videos
This applies for other pages like artistevents, artistphotos and artistbiography
Thank you in advance!

Try this :
RewriteRule ^([^/]*)/([^/]*)$ /artist$2?address=$1 [L]

Related

htaccess send content after ? as a parameter

This is my input URL
http://site_name/project/edit/sgdsg/getFile/?file=271bbec45d7855a332e6dfda85ca94b9.txt
I want it to be sent like
http://site_name/project/edit/sgdsg/getFile/271bbec45d7855a332e6dfda85ca94b9.txt
How can we do this? I want this only for the URL starting with http://site_name/project/edit/sgdsg/getFile/
So it don't affect any other URL.
This is what I have tried ...
RewriteRule ^project/edit/([A-Za-z0-9._-]+)/([A-Za-z0-9._-]+)/([A-Za-z0-9._-]+)/?$ modules-nct/edit_project-nct/index.php?slug=$1&action=method&method=$2&file=$3 [L]
RewriteRule ^project/edit/([A-Za-z0-9._-]+)/([A-Za-z0-9._-]+)/?$ modules-nct/edit_project-nct/index.php?slug=$1&action=method&method=$2 [L]
RewriteRule ^project/edit/([A-Za-z0-9._-]+)/?$ modules-nct/edit_project-nct/index.php?slug=$1 [L]
Please don't give it negative ratings just because my editing is improper.I am here to learn something new.
Thanks.
In your <a> tag:
<a href="your_url/your_other_parameters(means :project/edit/sgdsg/getFile/ )/directly enter your file name that you want to pass"> i.e your <a> tag must be
In htaccess:
RewriteRule ^project/edit/([A-Za-z0-9._-]+)/([A-Za-z0-9._-]+)/([A-Za-z0-9._-]+)$ modules-nct/edit_project-nct/index.php?slug=$1&action=method&method=$2&file=$3
In the above line, the href URL will be redirected to an original URL.

title redirecting in htaccess for redirecting url or something like this

i want to redirect this please guide me
form
http://example.com/babycare/general_help/category.php?id=10
to
http://example.com/babycare/article/category/10
the id should change deynamicly i.e id may b 10,12,or 23 etc
Blockquote
Try use on .htaccess:
RewriteRule ^babycare/article/category/([0-9]+)$ babycare/general_help/category.php?id=$1
try this one, you need to change some part of it accoriding to your category
RewriteEngine On
RewriteRule ^category/(.+)/(.+)$ category.php?$1=$2 [NC,L]

Rewrite htaccess URL base for multiple GET variables with PHP

I am looking to create a PHP file that can handle multiple GET variables and output data accordingly, however I would like to remove the base "category" URL element in order to shorten it, with htaccess, like so:
http://website.com/news/politics
http://website.com/videos/funny
http://website.com/posts/food
Currently I use different files for each type, but it is a pain:
RewriteRule ^news/([^/]+)/?$ tpl-news.php?slug=$1 [L,QSA,NC]
RewriteRule ^videos/([^/]+)/?$ tpl-videos.php?slug=$1 [L,QSA,NC]
RewriteRule ^posts/([^/]+)/?$ tpl-posts.php?slug=$1 [L,QSA,NC]
etc ...
Alternatively, I could use the following:
RewriteRule ^category/([^/]+)/?$ tpl-category.php?type=$1&slug=$2 [L,QSA,NC]
I do not want to have a URL like:
http://website.com/category/news/politics
http://website.com/category/videos/funny
http://website.com/category/posts/food
But would like to remove the "category" from the URL like so:
http://website.com/news/politics
http://website.com/videos/funny
http://website.com/posts/food
However other pages / URLs, for example like the ones below, should not be affected by this rule:
http://website.com/account/user
http://website.com/newspost/abcdef // a single post page
http://website.com/videopost/abcdef // a single video page
These "types" (news, videos, posts, ...) are limited in number, i.e. I have no objection predefining those, so that they do not conflict with other URLs of the page (such as image folders, admin sections, user profile sub pages, etc)
You can use this generic rule:
RewriteRule ^(news|videos|post)/([^/]+)/?$ tpl-category.php?type=$1&slug=$2 [L,QSA]
Or if you want to use existing .php files e.g. tpl-news.php, tpl-post.php etc then use:
RewriteRule ^(news|videos|post)/([^/]+)/?$ tpl-$1.php?slug=$2 [L,QSA]

mod_rewrite issues and php

I'm trying to convert my app links, so that a link like this:
http://localhost/index.php?id=13&category=Uncategorized&title=just-a-link
gets converted to this:
http://localhost/13/Uncategorized/just-a-test
so far I was able to do it using:
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)/([^/]*)$ /index.php?id=$1&category=$2&title=$3 [QSA,L]
but that completely breaks links to css and other files as it redirects every request with query to index.php
so I changed it slightly so that it only runs when the first query is a number:
RewriteEngine On
RewriteRule ^([0-9]*)/([^/]*)/([^/]*)$ /index.php?id=$1&category=$2&title=$3 [QSA,L]
this one doesn't break css and js files, however when you go to the link for example http://localhost/13/cat/test then try to go to another link like http://localhost/19/Uncategorized/something by clicking on it inside the previous page it will instead redirect you to something like http://localhost/13/Uncategorized/19/Uncategorized/just-a-test
How do I achieve what I described without any of these weird side effects??
Use :
RewriteEngine On
RewriteRule ^([0-9]+)/([^/.]+)/([^/.]+)/?$ /index.php?id=$1&category=$2&title=$3 [QSA,L]
And add this code to your html <header>:
<base href="/">
OR
<base href="http://www.domain.com/">
To fix relative css and js links

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

Categories