htaccess two vars to display two folders - php

I am trying to get my htaccess to rewrite the url to display two folders rather than two vars.
Here is my url:
www.MyWebsite.com/index.php?p=info&id=1234
That is actual url but I want it to display this:
www.MyWebsite.com/info/1234
Here is the code for the htaccess but it does not work for the second var.
DirectoryIndex index_good.html index.php
RewriteEngine On
RewriteBase /tjshow/
# RewriteRule ^([\w\-]+)/*$ ?p=$1 [L]
# RewriteRule ^([\w\-]+)/([\w\-]+)/*$ ?p=$1&id=$2 [L]
RewriteRule ^([\w\-]+)/*$ index.php?p=$1 [L]
RewriteRule ^([\w\-]+)/([\w\-]+)/*$ index.php?p=$1&id=$2 [L]
RewriteRule ^([\w\-]+)/([\w\-]+)/([\w\-]+)/*$ index.php?p=$1&id=$2&link=$3 [L]
The first one works fine but the second one is giving me a page but with all kinds of internal broken links and images.
This one works:
www.MyWebsite.com/index.php?p=info
And it displays this only, which is what I want it to do:
www.MyWebsite.com/info
But if I add the second var there like this
http://mywebsite.com/tjshow/second
Then the code breaks.

Try using this rule instead:
RewriteRule ^([^/]*)/([^/]*)$ /index.php?p=$1&id=$2 [L]
Using the above rule will leave you with the URL: www.MyWebsite.com/info/1234

Related

url mod rewriting multiple parameters

I have these rules that i want to make it work.
RewriteEngine on
RewriteRule ^/(view|show)/(ebook|lecture)/?$ page1.php?a=$1&b=$2 [L,QSA]
RewriteRule ^/(view|show)/(ebook|lecture)/([^/]+)/?$ page2.php?a=$1&b=$2&c=$3 [L,QSA]
RewriteRule ^/([^/]+)/-page-([0-9]+)?$ page3.php?a=$1&b=$2 [L,QSA]
My links are normally like this
www.domain.com/page1.php?a=view&b=ebook
www.domain.com/page2.php?a=view&b=ebook&c=title
www.domain.com/page3.php?a=title&b=6
and i want to turn them like the following look
www.domain.com/view/ebook //page1
www.domain.com/view/ebook/title //page2
www.domain.com/title/page-6 //page3
I've tried my rules but only the first one worked, But the page didn't load and the style was literally broken and not even a single image loaded or anything at all.
Try with:
RewriteEngine on
RewriteRule ^/?(view|show)/(ebook|lecture)/?$ page1.php?a=$1&b=$2 [L,QSA]
RewriteRule ^/?(view|show)/(ebook|lecture)/([^/]+)/?$ page2.php?a=$1&b=$2&c=$3 [L,QSA]
RewriteRule ^/?([^/]+)/page-([0-9]+)?$ page3.php?a=$1&b=$2 [L,QSA]
No leading slashes in left htaccess RewiteRule url (you can use RewriteRule ^(view...)
And you use only page- in your www.domain.com/title/page-6 //page3 link (not -page-)

htaccess won't work for directory

hello I am facing a problem with my htacess i have two php files that are used for URL rewriting first one is video.php and second one is channel.php. on homepage when I click any video its properly works for rewriting the url e.g.
mysite.com/video.php?url=1234
changes to that works properly for me
mysite.com/1234-video-custom-slug
but when I click for any channel it loads the same video.php but with rewriting urls of channel like:
mysite.com/channel/1-channel-slug
instead of rewriting urls from channel.php?id=1
I don't know what i am doing wrong here is my htaccess file code
RewriteEngine On
RewriteRule ^([a-zA-Z0-9-/]+)$ video.php?url=$1
RewriteRule ^([a-zA-Z0-9-/]+)/$ video.php?url=$1
RewriteRule ^channel/([a-zA-Z0-9-/]+)$ channel.php?id=$1
RewriteRule ^channel/([a-zA-Z0-9-/]+)/$ channel.php?id=$1
Change the order like this
RewriteEngine On
RewriteRule ^channel/([a-zA-Z0-9-/]+)$ channel.php?id=$1
RewriteRule ^channel/([a-zA-Z0-9-/]+)/$ channel.php?id=$1
RewriteRule ^([a-zA-Z0-9-/]+)$ video.php?url=$1
RewriteRule ^([a-zA-Z0-9-/]+)/$ video.php?url=$1

Rewrite domain, but still use index.php

Current code to use index.php for all urls :
RewriteRule ^(.*)$ ./index.php
But for one special case, I want to make url
http://domain.com/s/this_changes to call actually
http://domain.com/steps/step/this_changes.
I tried adding :
RewriteRule ^./s/$ ./steps/step/$1, but it gives an error 500.
Try this:
RewriteEngine On
RewriteRule ^s/(.+)$ /steps/step/$1 [L]
Another way you could do it is to route all your URIs through index.php, and create a function for each one.
RewriteRule ^s/this_changes$ steps/step/this_changes
if it is that simple or
RewriteRule ^(.*)/(.*)$ steps/$1/$2
And your specific code would work like this
RewriteRule ^s/(.*)/?$ ./steps/step/$1
The (.*) is referenced in your $1 in the second part of your code.

Rewrite rules not working htaccess

I have the following htaccess rewrite rules
RewriteRule ^shows-watch/(.*).html?$ show.php?name=$1
RewriteRule ^shows-watch/(.*)/season-(.*).html?$ show.php?name=$1&season=$2
RewriteRule ^shows-watch/(.*)/season-(.*)/episode-(.*).html?$ show.php?name=$1&season=$2&episode=$3
Now the thing is, the first rewrite rule works just fine
RewriteRule ^shows-watch/(.*).html?$ show.php?name=$1
It's just when I try using the others, only name get variable is being passed and not
$_GET['season']
or
$_GET['episode']
i know it's most likely something simple I'm missing or have done, but I just can't seem to get it working.
Give this a try:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteRule ^shows-watch/([^/]+)/season-([^/]+)/episode-([^/]+).html?$ show.php?name=$1&season=$2&episode=$3 [QSA,NC,L]
RewriteRule ^shows-watch/([^/]+)/season-([^/]+).html?$ show.php?name=$1&season=$2 [QSA,NC,L]
RewriteRule ^shows-watch/([^.]+)\.html?$ show.php?name=$1 [QSA,NC,L]
The order is very important so they don't overlap you also need the L flag to stop when needed.
This assumes your .htaccess is on the root folder of your domain along with the show.php file and that you are accessing it like this:
domain.com/shows-watch/Show Name.html
domain.com/shows-watch/Show Name/season-1.html
domain.com/shows-watch/Show Name/season-1/episode-10.html
The first line gets all the links because it matches all the links.
Try reverse the order:
RewriteRule ^shows-watch/(.*)/season-(.*)/episode-(.*).html?$ show.php?name=$1&season=$2&episode=$3
RewriteRule ^shows-watch/(.*)/season-(.*).html?$ show.php?name=$1&season=$2
RewriteRule ^shows-watch/(.*).html?$ show.php?name=$1
Or you can exclude slashes like this:
RewriteRule ^shows-watch/([^/]*).html?$ show.php?name=$1
RewriteRule ^shows-watch/([^/]*)/season-([^/]*).html?$ show.php?name=$1&season=$2
RewriteRule ^shows-watch/([^/]*)/season-([^/]*)/episode-([^/]*).html?$ show.php?name=$1&season=$2&episode=$3

Use htaccess for a redirect

I am using the following code to change all my pages from .php to a simple /
RewriteRule ^(.*)/$ $1.php [L]
Now come up as www.site.com/clubs/ or www.site.com/training/ instead of www.site.com/clubs.php and www.site.com/training.php
Now within clubs i want to show the clubs (using data from a database) and the page will show as www.site.com/clubs/my-club/
All the my-club data will be in the database including the link in a url field.
I can get this with the following code. However when i echo using the $_GET['club'] method it is showing up my-club.php instead of just my-club.
RewriteRule ^clubs/([^/]*)$ clubs.php?url=$1 [L]
The ending / in the url seems to get changed into .php
You can achieve what you need in different ways, however that's how I would do it.
Change rules order, like this:
RewriteRule ^clubs/([^/]*)/$ clubs.php?url=$1 [L]
RewriteRule ^(.*)/$ $1.php [L]
I added the trailing slash to the first rule, since I guess is part of the pattern. If I'm wrong about it you can just delete it.
If you want to append any query string from the original request URL you should use QSA together with L, so [L,QSA]
Try this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9a-zA-Z\-]+?)/([0-9a-zA-Z\-]+?)?$ $1.php?url=$2
Whan are you run:
http://localhost/club/
result will be
http://localhost/club.php
Whan are you run:
http://localhost/club/my-club
result will be
http://localhost/club.php?url=my-club

Categories