mod_rewrite not making my url pretty - php

I know this might be a simple fix but I cannot figure it out.
I have my .htaccess file in folder called videos that is in my root file.
I wrote this rewrite mod:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}.php -f
RewriteRule ^(.*)$ $1.php [L]
RewriteRule ^videos/([a-z0-9\-]+)$ vid.php?=$1 [L]
</IfModule>
For some reason my url still comes up like this:
http://localhost/fap2/videos/vid.php?id=1
It does not replace that ?id=1 with just the number 1 like vid.php/1
Also since that vid.php?id=1 is from a href on index.php, and It looks like this:
<a href="videos/vid.php?id=' . $tag["video_tag"] .'">
my pages are based on that id. Can I somehow show the id category name instead of a numer so for example that vid.php?id=1 be that actual name vid.php/category1 or is that based on changing that a href tag?
Thank you

try this
RewriteEngine on
RewriteBase /fap2/
RewriteRule ^videos/([^/]+) vid.php?id=$1 [L]

Related

.htaccess Redirect any file in directory to index.php of same directory

I'm creating a blog, and would like to use the title of each entry as the URL.
The blog is pulling from a database and has code in a single php file to display the blog entry.
I would like any url like, domain.com/blog/this-is-the-title.html to redirect to
domain.com/blog/index.php
BUT, keep the URL in the browser bar as the original url.
EDIT...
domain.com/blog/anything-that-is-here.html
should redirect to domain.com/blog/index.php
But, still show domain.com/blog/anything-that-is-here.html in the browser address bar.
I hope this makes sense.
Hoping this is something that can be accomplished in.htaccess.
Thanks!
Rick
You are searching for the rewrite function from Apache.
Something like this should work for your case:
RewriteEngine on
RewriteBase /blog/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?title=$1
An URL like domain.com/blog/test-title will internally call your index.php with $_GET['title'] = "test-title".
Try using [P]
RewriteEngine on
RewriteRule ^(.*)$ / domain.com/blog/index.php [P]

How to Clean URL using .htaccess

I am very frustrated looking for working clean URL using .htaccess:
My .htaccess code:
RewriteEngine on
RewriteRule ^([^/\.]+)/?$ $1.php [L,QSA]
RewriteRule ^/([0-9]+)$ product.php?id=$1
The first rule is percfectly working in hiding the .php extension of my URL but the second rule is not working. The URL of my product after clicking the product ID no on the product list. I'm using free host for now. freehost.16mb.com/product?id=156
My Code of my product list link is:
link 1 array
So I'll try to clean the URL using .htaccess into freehost.16mb.com/product/156.
I am using the .htaccess code above but it doesn't work for me. I need some help.
Your second rule does not make the provision for the leading /product before the ID, and your HTML anchor is using the format that you are rewriting to - this needs to be swapped around.
Change your anchor tag to the following:
<a href="/<?php echo $row['ID']; ?>
You also need to fix your rule up a bit by adding the QSA and L flags, and by removing the leading slash from the rule's pattern:
RewriteRule ^([0-9]+)$ product.php?id=$1 [QSA,L]
Once you've done that, /1234 will rewrite to /product.php?id=1234.
However, if you wish for the /product segment to be included, then use these instead:
<a href="/product/<?php echo $row['ID']; ?>
RewriteRule ^product/([0-9]+)$ product.php?id=$1 [QSA,L]
Just Use this in your .htaccess:
Options +FollowSymLinks
RewriteEngine on
RewriteRule product/id/(.*)/ product?id=$1
RewriteRule product/id/(.*) product?id=$1
This will turn your URL into:
freehost.16mb.com/product/id/156
To have freehost.16mb.com/product/156 just remove the id/ from the RewriteRule, so it will be:
Options +FollowSymLinks
RewriteEngine on
RewriteRule product/(.*)/ product?id=$1
RewriteRule product/(.*) product?id=$1
---- ALTERNATIVE ----
RewriteCond %{QUERY_STRING} (^|&)id=156($|&)
RewriteRule ^freehost\.16mb\.com/product$ /freehost.16mb.com/product/156?&%{QUERY_STRING}

changing url structure with htaccess

i have a url
http://domain.com/wallpaper-name-of-wallpaper-id.html
where wallpaper- is prefix of the url and name-of-wallpaper is title of the wallpaper while id is the actual id of the wallpaper. my current .hataccess file looks like.
RewriteRule ^wallpaper-([^/]*)\.html$ wallpaper.php?permalink=$1 [L]
but i want to change it to
http://domain.com/wallpaper/name-of-wallpaper-id.html
so user who will enter the old url will automatically sent to the new url with htaccess.
i have tried.
RewriteRule ^wallpaper-([^/]*)\.html$ wallpaper/wallpaper.php?permalink=$1 [R,L]
but don't seems to work for me. any idea or help?
Add a new redirect rule before existing rule:
Options -MultiViews
RewriteEngine On
RewriteRule ^(wallpaper)-([^.]+\.html)$ /$1/$2 [R=302,L,NC]
RewriteRule ^wallpaper/([^.]+)\.html$ wallpaper.php?permalink=$1 [L,NC,QSA]

Static url does not redirect to the requested page

I have a question regarding the .htaccess file.
I have rewritten a dynamic url in a static one as following:
www.mysite.com/index.php?lang=IT
in
www.mysite.com/it
using this rewrite rule:
RewriteEngine On
RewriteRule ^it/([^/]*)\.html$ /index.php?lang=$1 [L]
So far so good...
Now I need to create another static url for a dynamic url which redirects to the product description
www.mysite.com/product_details.php?id=1485
The rule inserted in the .htaccess is:
RewriteEngine On
RewriteRule ^it/prodotto-([^-]*)-([^-]*)\.html$ /it/product_details.php?id=$1&lang=$2 [L]
Unfortunately if I click on the related link I cannot reach the requested page; but if I comment the first rule like this
#RewriteRule ^it/([^/]*)\.html$ /index.php?lang=$1 [L]
then I can see the page.
Do you know how to solve this issue?
Assuming that lang equals to language you want the script do like so?
RewriteEngine On
RewriteBase /
RewriteRule ^it/([^/]*).html$ /index.php?lang=$1 [L]
RewriteRule ^it/prodotto-([^/]*)-([^/]*).html$ /it/product_details.php?id=$1&lang=$2 [L]
this will do
yoursite.com/it/$1.html
and
yoursite.com/it/prodotto-$1-$2.html
?

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

Categories