htaccess rewriting rules not working - php

I am trying to rewrite the following url
http://localhost/foldername/index.php?url=writeboard/index&step=1&id=1
to
http://localhost/foldername/writeboard/index/step/1/id/1
Code is
RewriteRule ^(.+?)(?:/(step)/([0-9]+))?/?$ index.php?url=$1&$2=$3 [NC,L,QSA]
I tried with
RewriteRule ^(.+?)(?:/(step)/([0-9]+))(?:/(id)/([0-9]+))?/?$ index.php?url=$1&$2=$3&$4=$5 [NC,L,QSA]
it works but when the url becomes http://localhost/foldername/writeboard/index then I am getting 404.

This rule should work:
RewriteRule ^(.+?)/(step)/([0-9]+)/(id)/([0-9]+)/?$ index.php?url=$1&$2=$3&$4=$5 [NC,L,QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+?)/?$ index.php?url=$1 [NC,L,QSA]

You should also add RewriteBase /foldername/ to your .htaccess file, since you're not in the top level.
More information about RewriteBase can be found here.

Related

HTACCESS Not Picking Up The Path

I'm using HTACCESS in my website but I'm facing a small problem with one of the files/links.
My link is http://localhost/photos/view/1465574353
My HTACCESS is:
Options -MultiViews
DirectoryIndex index.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/?$ index.php?category=$1 [NC,L,QSA]
RewriteRule ^([\w-]+)/([0-9]+)/?$ index.php?category=$1&currentpage=$2 [NC,L,QSA]
RewriteRule ^view/([0-9]+)/?$ view.php?id=$1 [NC,L,QSA]
When I follow the link I am not redirected anywhere at all, yet when I follow http://localhost/photos/view.php?id=1465574353 I am shown the page.
Anyone know the reason why this may be?
** If I change it to
RewriteRule ^view/([0-9]+)/([\w-]+)/?$ view.php?id=$1&title=$2 [NC,L,QSA]` and visit my page at `localhost/photos/view/1465574353/title
I am shown the correct page!
FYI: I'm using the same format for my other section, which looks like this:
Options -MultiViews
DirectoryIndex index.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/?$ index.php?section=$1 [NC,L,QSA]
RewriteRule ^([\w-]+)/([0-9]+)/?$ index.php?section=$1&currentpage=$2 [NC,L,QSA]
RewriteRule ^read/([0-9]+)/([\w-]+)/?$ read.php?id=$1&title=$2 [NC,L,QSA]
http://localhost/photos/view/1465574353 is already matched by
RewriteRule ^([\w-]+)/([0-9]+)/?$ index.php?category=$1&currentpage=$2 [NC,L,QSA]
view - ([\w-]+)
1465574353 - ([0-9]+)
and therefore not handled by the final RewriteRule.
If you want it handled by the more specific (view) rule, you must swap the last two, because the rules are tried in order.
RewriteRule ^view/([0-9]+)/?$ view.php?id=$1 [NC,L,QSA]
RewriteRule ^([\w-]+)/([0-9]+)/?$ index.php?category=$1&currentpage=$2 [NC,L,QSA]

How to rename part of a url with .htaccess?

I currently have very little Apache experience, and am having difficulties with my .htaccess file. My question is this: how can I rename these files, listed below, properly? I believe my syntax is accurate, according to http://www.htaccesscheck.com, but when accessing these pages, either A: the page won't load due to a redirect loop, or B: the page won't load, but will redirect to the wrong page. Here is my current .htaccess file for this directory:
RewriteEngine On
RewriteBase /photos/
RewriteRule ^(.*)-(.*)$ archives.php?month=$1&year=$2 [L]
RewriteRule ^(.*)$ catpost.php?id=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteRule ^(.*)$ viewpost.php?id=$1 [QSA,L]
Any help is much appreciated.
Try code below:
RewriteEngine on
RewriteBase /photos/
RewriteRule ^([0-9]{1,2})-([0-9]{4})$ archives.php?month=$1&year=$2 [L]
RewriteRule ^([0-9]+)$ catpost.php?id=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteRule ^(.*)$ viewpost.php?id=$1 [QSA,L]
This rules will check, if:
request like yourdomain/11-1111, then return archives.php
request like yourdomain/111, then return catpost.php (you can type
any number)
else will return viewpost
You have some errors in your current .htaccess, because your second rule get result of first rule.
By the way, you can use http://htaccess.madewithlove.be/ to check step by step what is posted to your rewrite rules.
be sure to write a valid pattern
try this
RewriteEngine On
RewriteBase /photos/
RewriteRule ^(.*?)-(.*?)$ archives.php?month=$1&year=$2 [L]
RewriteRule ^(.*?)$ catpost.php?id=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteRule ^(.*?)$ viewpost.php?id=$1 [QSA,L]

HTACCESS directories redirect

I'm trying to redirect the address
www.example.com/tops/articles/first_article
to the address
www.example.com/tops/test1.php?name=first_article
also to redirect the address
www.example.com/tops/galleries/first_gallery
to the address
www.example.com/tops/test2.php?name=first_gallery
and all other addresses like
www.example.com/tops/first_page
to
www.example.com/tops/page.php?name=first_page
The following htaccess file is giving me a redirect loop.
RewriteEngine on
Options +FollowSymLinks
RewriteRule ^/tops/articles/(.+)$ /tops/test1.php?name=$1 [L]
RewriteRule ^/tops/galleries/(.+)$ /tops/test2.php?name=$1 [L]
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule (.*)$ ./page.php?name=$1
Doe's anyone has any idea why is that?
What am I doing wrong?
Thanks in advance.
Does page.php exist in the root directory (or any other non /tops/ directory for that matter)?
If not, then that is probably your problem. You should change your last rule to something like:
RewriteRule ^tops/(.*)$ /tops/page.php?name=$1
Well, I finally got it working.
Thought I share it so if anyone every search for this question he will find it.
Here is the code that workd:
RewriteEngine on
Options +FollowSymLinks
RewriteRule ^articles/(.+)$ ./test1.php?name=$1 [L]
RewriteRule ^galleries/(.+)$ ./test2.php?name=$1 [L]
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule (.*)$ ./page.php?name=$1 [L]
Enjoy !
You may try this:
RewriteEngine On
RewriteRule ^tops/articles/([a-zA-Z0-9-=_.]+)/?$ http://www.example.com/tops/test1.php?name=$1 [L]
RewriteRule ^tops/galleries/([a-zA-Z0-9-=_.]+)/?$ http://www.example.com/tops/test2.php?name=$1 [L]
RewriteRule ^tops/([a-zA-Z0-9-=_.]+)/?$ http://www.example.com/page.php?name=$1 [L]
Replace all 3 rules with these rules.
UPDATED for all characters:
RewriteEngine On
RewriteRule ^tops/articles/([^/]*)/?$ http://www.example.com/tops/test1.php?name=$1 [L]
RewriteRule ^tops/galleries/([^/]*)/?$ http://www.example.com/tops/test2.php?name=$1 [L]
RewriteRule ^tops/([^/]*)/?$ http://www.example.com/tops/page.php?name=$1 [L]
.htaccess should be in root directory If it is in /tops directory, try removing it from the pattern and the substitution URL.

How to correct this rewrite rule?

I have url as
http://www.mydomain.com/levels/home?mode=48bb6e862e54f2a795ffc4e541caed5c. I need to change this url to http://www.mydomain.com/medium.
I am not familiar with rewrite url.
I tried with RewriteRule ^medium/?$ levels/home?mode=48bb6e862e54f2a795ffc4e541caed5c, but not worked correctly.
Full rewrite rule
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^medium/?$ levels/home?mode=48bb6e862e54f2a795ffc4e541caed5c
RewriteRule ^(.*)$ index.php [QSA,L]
Try to change only that line to:
RewriteRule ^medium /levels/home?mode=48bb6e862e54f2a795ffc4e541caed5c
Or if you don't care if anything is after medium is the following:
RewriteRule ^medium(.*) /levels/home?mode=48bb6e862e54f2a795ffc4e541caed5c
Your rules are in the wrong order, try changing them to:
RewriteEngine On
RewriteRule ^medium/?$ levels/home?mode=48bb6e862e54f2a795ffc4e541caed5c
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]
The conditions need to be applied to the index.php rule, and not the medium rule. RewriteConds are only applied to the immediately following RewriteRule, not all of them.
This rewrite rule worked fine
RewriteRule ^medium.*$ /levels/home?mode=48bb6e862e54f2a795ffc4e541caed5c [R=301,L]

Turn mod_rewrite rules into PHP rules

I want to be able to handle mod rewrites from within PHP instead of my .htaccess file, this way when I have custom modules that need a new rewrite I don't have to redo the .htaccess file.
I want to mimic the way that WordPress does their .htaccess which is:
RewriteEngine On RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ /index.php/$1 [L,QSA]
My current .htaccess is this:
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# MBP Rules
RewriteRule ^module/([A-Za-z-_]+)/([A-Za-z-_]+)/?$ /index.php?p=module&prefix=$1&module_page=$2 [NC,QSA,L]
RewriteRule ^module/([A-Za-z-_]+)/([A-Za-z-_]+)/([A-Za-z-_]+)/?$ /index.php?p=module&prefix=$1&module_page=$2&page=$3 [NC,QSA,L]
RewriteRule ^module/([A-Za-z-_]+)/([A-Za-z-_]+)/([0-9]+)/?$ /index.php?p=module&prefix=$1&module_page=$2&id=$3 [NC,QSA,L]
RewriteRule ^([A-Za-z-_]+)/?$ /index.php?p=$1 [NC,QSA,L]
RewriteRule ^([A-Za-z-_]+)/([A-Za-z-_]+)/?$ /index.php?p=$1&s=$2 [NC,QSA,L]
RewriteRule ^([A-Za-z-_]+)/([A-Za-z-_]+)/([0-9]+)/?$ /index.php?p=$1&s=$2&id=$3 [NC,QSA,L]
Does anyone know how to make this happen?
My way is to replace
RewriteRule ^(.+)$ /index.php/$1 [L,QSA]
With
RewriteRule ^(.+)$ /index.php?path=$1 [L,QSA]
Then you have to do similar parsing and everything as modrewrite does, but you can do it yourself using preg_match on $_GET['path']. I.e.
if (preg_match('/id/([0-9]*)', $_GET['path'], $matches)) {
Do code;
}
Sure, have the .htaccess file look like the first example you have, and build your script from there.
Probably write some sort of Router class, to route the requests to their places, the fundamentals being splitting the received query by /, which gives you an array of URL parts, and start conditioning.

Categories