url rewriting with .htaccess - php

I'm trying to rewrite the url on a site I have on localhost (port 8000), I have already set this up for certain other directorys but I can't manage to do it for this one. I compared it to several other working .htaccess files but there wasn't any difference. (The module is activated.) The site is in a subdirectory called HTF and the file single.php works just fine. The htaccess file is placed in the same directory.
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteRule videos/([0-9]+)/$ /HTF/single.php?id=$1 [L]
I would like to get an url like localhost:8000/HTF/video/1232434 for exemple.
Any help appreciated, tested several types of htacces files, here a working exemple for a other subdirectory called sorted:
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteRule ^image-post/([0-9]+)/(.+)\.php$ /sorted/image-post.php? id=$1&title=$2 [L]
RewriteRule ^pages/([0-9]+)/(.+)\.php$ /sorted/page.php?page_number=$1&cat=$2 [L]

Your rule will match videos, not video as you were wanting. Also it will only match with a trailing slash.
This will work
RewriteEngine on
RewriteRule ^video/([0-9]+)/?$ single.php?id=$1

localhost:8000/HTF/video/1232434
will not match
videos/([0-9]+)/$
because of the trailing slash /. Try /? to make the slash optional or leave away. And videos.
^/HTF/video/([0-9]+)/?$ /HTF/single.php?id=$1 [L]
or
^/HTF/video/([0-9]+)$ /HTF/single.php?id=$1 [L]
This shall do the job. Sorry can not try here.

Related

Rewrite Rule for subfolder instead of main website

I have a htaccess code that i want to be changed.
RewriteEngine on
RewriteBase /
RewriteRule ^(.*)$ proxy.php?url=$1 [L,QSA]
It works like a charm if i place proxy.php and .htaccess file in main website and visit pages through www.domain.com/
I want to change this RewriteRule in a way that if I place the proxy.php and htaccess in subfolder named "folder" then new results show in www.domain.com/folder/
So mainly purpose is to change results path from www.domain.com to www.domain.com/folder/
You can place your code without any changes in /folder/.htaccess with proxy.php.
Or you can use:
RewriteEngine on
RewriteBase /
RewriteRule ^folder/(.+)$ folder/proxy.php?url=$1 [L,QSA]

How to use ReWriteBase in this scenario?

Here is my .htaccess file.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /~new/
</IfModule>
Directory structure of my website is this:
Now what I want to do is to be able to visit paths like following:
domain.com/~new/pages/page1.php
But when I try to visit above URL I get redirected to 404 page.
What am I missing? Are ReWriteRule also needed?
RewriteBase doesn't do what you think it does. In fact, by itself it does nothing. RewriteBase is only necessary if you have relative path substitutions in your RewriteRule. And if you aren't doing that (ie. you are using root-relative or absolute paths) then you don't actually need it.
Are ReWriteRule also needed?
Yes, RewriteRule is what actually does the URL rewriting. And you need to do some URL rewriting.
So, basically, you want to internally rewrite your URLs from:
example.com/~new/pages/page1.php (Your "virtual" URL)
to example.com/pages/page1.php (The real URL)
RewriteRule ^~new/(.*) /$1 [L]
The (.*) captures everything after /~new/ and $1 is a backreference to this captured subpattern.

how to rewrite single URL using .htaccess

I need to rewrite only 1 specific URL, to display to visitors specific content: I tried something like, this:
RewriteEngine on
RewriteCond %{REQUEST_URI} example.com/test/2_5/page.html
RewriteRule ^(.*)$ example.com/tt.html [R,L]
I need to rewrite all requests to:
http://example.com/test/2_5/page.html
to
http://example.com/tt.html
how to do this?
thanks,
Redirect /test/2_5/page.html /tt.html
Ok, mod_rewrite
RewriteRule ^/test/2_5/page.html /tt.html [L]
Remove first / if using .htaccess in the site's root folder, not the .conf file. And, typically, the final url should be the full one, with http:// and domain, but this one will work too. If you want to do everything by the rules then
RewriteRule ^/test/2_5/page\.html$ http://example.com/tt.html [L]

rewrite url doesn't include layout?

I'm currently trying to make SEO friendly url's for my website using this script:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^show/([0-9]+)/$ show.php?phto=$1
RewriteRule ^index/$ index.php
So i've tested some things, and i've came up with these problems:
When i visit my website : blalba.com/index/ my layout file wouldn't include/show up. (using an layout.inc.php header/footer system)
Also how can i make it that the user can visit it with index and index/
I'm not so good at this..
Grz
Change your rule with this code (to make trailing slash optional):
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteRule ^show/([0-9]+)/?$ show.php?phto=$1 [L,QSA,NC]
RewriteRule ^index/?$ index.php [L,QSA,NC]
Also make sure for including style, js, images etc always use absolute path i.e. it should start with / or http://.

mod_rewrite with Trailing Slash breaks CSS/IMG/SCRIPTS paths

I'm trying to make mod_rewrite the first sub-directory string from url in order to create similar functionality as 'jsfiddle.net saved url's within a class/db. The script works fine and does the rewrite.
e.g. of url
http://jsfiddle.net/RyEue/
This works fine (loads all css, scripts, etc.):
http://www.domain.com/787HHJ2
This is what I've used in the past which does the trick.
The problem Is when ending URL with last slash, script, css and others loose path.
http://www.domain.com/787HHJ2/
rewrite script:
DirectoryIndex index.php index.html
Options +FollowSymlinks
RewriteEngine On # Turn on the rewriting engine
#RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{QUERY_STRING} !.
RewriteRule ^.+/?$ index.php [QSA,L]
Unsure if this has to do with Rewritebase, I've tried multiple ways.
PS. I've tried setting paths to absolute (e.g. src="/img/theimage.jpg") without luck.
1. Make sure you have css/images/js etc linked relative to root folder (with leading slash): /styles/main.css
2. Add one of these ruls before current one:
# do not touch files with .css/.js/.jpg etc extensions
RewriteRule \.(css|js|jpg|png|gif)$ - [L]
or
# do not touch any resources in images/css/js folders
RewriteRule ^(images|css|js)/ - [L]
3. Clear browser caches and restart (sometimes browser may display cached page/resource when rewrite rule was fixed which brings a lot of confusion).
Try escaping
RewriteRule ^.+\/?$ index.php [QSA,L]

Categories